try {
aSession = getSession();
tx = aSession.beginTransaction();
SbiObjects hibBIObject = (SbiObjects)aSession.load(SbiObjects.class, obj.getId());
SbiObjectsRating hibBIObjectsRating = new SbiObjectsRating();
hibBIObjectsRating = loadBIObjectRatingById(obj, userid);
Integer newRating = new Integer(rating);
if(newRating!= null && newRating.intValue()> 5){
newRating = new Integer(5);
}else if(newRating!= null && newRating.intValue()< 0){
newRating = new Integer(0);
}
if (hibBIObjectsRating != null){
hibBIObjectsRating.setRating(newRating);
updateSbiCommonInfo4Update(hibBIObjectsRating);
aSession.update(hibBIObjectsRating);
}else {
SbiObjectsRating hibBIObjectsRating1 = new SbiObjectsRating();
SbiObjectsRatingId hibBIObjectsRatingId1 = new SbiObjectsRatingId();
hibBIObjectsRatingId1.setObjId(obj.getId());
hibBIObjectsRatingId1.setUserId(userid);
hibBIObjectsRating1.setId(hibBIObjectsRatingId1);
hibBIObjectsRating1.setRating(newRating);
hibBIObjectsRating1.setSbiObjects(hibBIObject);
hibBIObjectsRating = hibBIObjectsRating1 ;
updateSbiCommonInfo4Insert(hibBIObjectsRating);
aSession.save(hibBIObjectsRating);