-->
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.  [ 6 posts ] 
Author Message
 Post subject: unsaved-value question
PostPosted: Fri Sep 26, 2003 3:47 am 
Beginner
Beginner

Joined: Wed Sep 24, 2003 8:27 am
Posts: 36
The documentation states:-



Top
 Profile  
 
 Post subject: Re: unsaved-value question
PostPosted: Fri Sep 26, 2003 4:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
suchakjani wrote:
The documentation states:-
I want unsaved-value="any" to go to save(). How can i do this ?


Does not make sense to me. Why not have it unsaved-value="none"?

Composite keys are an object and thus its best to use a NULL as the unsaved value (which is the default for H2).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 5:28 am 
Beginner
Beginner

Joined: Wed Sep 24, 2003 8:27 am
Posts: 36
Quote:
Does not make sense to me. Why not have it unsaved-value="none"?

Composite keys are an object and thus its best to use a NULL as the unsaved value (which is the default for H2).


The reason is i am creaing the composite key myself and assigning the values for new objects, so i want to save them when they have any value.

But Objects loaded from the db will also have values, but they have to be updated.

so i guess that leave me with one option and that is to implement imement the Interceptor interface isUnsaved() method.

In that, i do not want to implemnt the whole interceptor interface but only the isUnsaved() method. I still do not know how i can do this.

Regards
Suchak Jani


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 7:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I was thinking the question was for 1) synthetic and then 2) composite keys.
When using composite keys the key generation needs to be assigned. The interceptor can be implemented with the unused mthods as stubs. Most IDEs will build the class for you so you only need to add a few lines of code.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 8:10 am 
Beginner
Beginner

Joined: Wed Sep 24, 2003 8:27 am
Posts: 36
David,

Thanks much for your reply.

Quote:
When using composite keys the key generation needs to be assigned.


Ok, does this mean that i have to use some kind of generator for composite keys and i cannot assign them based on some logic i have.

Quote:
The interceptor can be implemented with the unused mthods as stubs.
Most IDEs will build the class for you so you only need to add a few lines of code.


Ok, so there are two ways i can set the interceptor:-

Code:
SessionFactory.openSession(myInterceptor),
Configuration.setInterceptor(myInterceptor)


Assume that
Code:
myInterceptor
implements
Code:
Interceptor
.

Now when i do this and myInterceptor does not implement/does not give the right return values for the other methods like ...

Code:
boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
          Called before an object is saved.


Quote:
The above is from net.sf.hibernate.Interceptor


What will happen ? will these methods still be called by hibernate ? I do acknowledge my complete ingorance of the Interceptor.

I use Eclipse and for
Code:
onSave()
it gives me this stub:-

Code:
   /* (non-Javadoc)
    * @see net.sf.hibernate.Interceptor#onSave(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
    */
   public boolean [quote]onSave[/quote](
      Object arg0,
      Serializable arg1,
      Object[] arg2,
      String[] arg3,
      Type[] arg4)
      throws CallbackException {
      // TODO Auto-generated method stub
      return false;
   }


Here is what the javadoc says for this method :-

Code:
onSave
public boolean onSave(Object entity,
                      Serializable id,
                      Object[] state,
                      String[] propertyNames,
                      Type[] types)
               throws CallbackExceptionCalled before an object is saved. The interceptor may modify the state, which will be used for the SQL INSERT and propagated to the persistent object.
Returns:true if the user modified the state in any way. CallbackException

I frankly do not understand wether my stub for the above method is ok or not.

As far as i can see, all i need to implement only
Code:
isUnsaved()
method.

Thus, any examples, usage documentation will be highly appreciated.(I did read the manual though)

Also now David, comimg back to the First question, let's say i implement a Interceptor, will i be able to now use it for the composite objects as well ?

Another question.

Quote:
Assume here that all the objects in the statements below have auto generated keys.


What about a cacade where i may have the parent object in the database and the child objects may or may not be already in the database, that is, some new children which need a save and some children from the database which need a update? Will the Interceptor work then.

What if the child has a one to many with another child, which has a one to many with another child. and the children at ay level, can be, some new children which need a save and some children from the database which need a update.

Please do point out if i am not making things clear.

To sum up there are three questions:-
1) If i stub Interceptor methods can there be any potential negative side effects.
2) Can i use a Interceptor with composite keys, where i have some application logic fill up the keys.
3) Can i use a Interceptor with a n level cascade collection, where some objects are for save and some for update ? SaveOrUpdate();

Thanks for all the help.

Again, if i am being unclear do point out the same.

Regards
Suchak Jani


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 9:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Lets just answer a few questions not so many as it hard to track them and our quoting gets wide.
1) assigned for the generateor of the primary key means you the developer will allocate the keys, eg, the composite class.
2) The onSave stub is fine but I always suggest you try it as you will learn more that way. No side affects if the defaults are used.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.