-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: session.save(String,Object)
PostPosted: Thu Jan 04, 2007 2:49 am 
Newbie

Joined: Thu Jan 04, 2007 2:31 am
Posts: 2
Hi all,

I wanted to know the exact use of Session.save(String, Object) API. I was looking towards it because of following requirement. Any other suggestions are also welcome.

Requirement:

I have one hibernate object OBJ having fields
1. String id (primary key, auto generated) : hence getter would be public and setter is private.
2. String refId(is unique always for every record)!
3. String name

I have to write an API like createOrUpdate(OBJ myObj)

myObj will be populated and passed by the caller. The id would be null but refId, name would be populated always.

Since refId is unique, hence based upon this i have to either create or update. If any record for myObj.getRefId() exists in the table, i need to update the row with the values of passed myObj else i need to create a new record.

Problem : Create its fine, problem is with update!Lets say we find one record with the passed refId. Now i need to update with values based upon myObj.

i have an object myObj but this is having id as null (and since setId cannot be used as its private, it will always be null). If i use this in session.save(myObj), a new record will be created.But i need update!
I have retrieved object based upon refId. I can get the id also from here but i cannot set it to the passed myObj.

Question : How do i update myObj to the table?

1. Either i get/set all the values from myObj to the retrieved object which is very combursome(lots of fields are present and same type of APIs i have to write for many other types of BOs)(I cannot use reflections since in our project its not allowed.... :-( )
2. I saw session.save(String,Object) but i was not able to understand this API! Is the string the value of primary key and the Object the object that needs to be updated based upon the passed primary key value?
3. Is there any other API which i can use where i can specify the BO and the primary key value?

Thanks in advance
Adwitiya Nigam...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 3:20 am 
Regular
Regular

Joined: Thu Aug 17, 2006 4:50 am
Posts: 55
Location: Mallorca
In Hibernate v2 ( and now in the classic package ) the previous version of Session has a method :


Code:
void save(Object object, Serializable id)


Just to do what you want. Know this method is deprecated but maybe you are interested in using it until you have a better solution.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 3:54 am 
Newbie

Joined: Thu Jan 04, 2007 2:31 am
Posts: 2
Naah....cannot use deprecated APIs.
Saw this API in classic package's Session first and then went to the new Session.save API :-)!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 4:59 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Why do you want to reimplement what Hibernate already does as an additional layer? Why could you receive an entity with its id valued to null if it has already been saved?

Hibernate already uses the id to know if the object is to be INSERTed or UPDATEd. For this purpose, it uses the unsaved-value attribute on the id tag (defaulted to null) => if the id is null, try and INSERT, if the id is not null, then just issue an UPDATE.

This is close to what you want to do, although what you want is closer to the Session.merge() semantics: you'll always issue a SELECT to know what to do then.

If you want to do what you said anyway, imo the problem of calling setId() is "anecdotal". In fact, if you really need to set a private field, or to call a private method, use reflection, I can't see a lot of other ideas to do it... By the way, this is exactly what Hibernate can do: by default, you can declare your getters/setters protected, Hibernate won't have any problem to use it. You can also ask Hibernate to directly access fields even if there's no getter/setter (set access="field" attribute).

HTH

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.