-->
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.  [ 8 posts ] 
Author Message
 Post subject: newbie: POJO gets id but is not saved
PostPosted: Wed Dec 07, 2005 12:33 pm 
Newbie

Joined: Tue Oct 18, 2005 8:43 am
Posts: 15
hello,
I have two basic questions regarding persistent objects:

Question 1:
I create a POJO of type A. After that, I create an arbitrary query.

Code:
A myPojo = new A();
Query q = session.createQuery("from ....");


The createQuery() causes Hibernate to flush. myPojo gets an ID assigned, but is apparently not persisted to the database. How come? Isn't flushing supposed to save the objects to the database?

Question 2:
Later in my code, I try to do saveOrUpdate() on myPojo.

Code:
session.saveOrUpdate(myPojo);
transaction.commit();


Since myPojo already has an ID, I get an exception telling me I have a foreign-key problem (ORA-02291: integrity constraint xxx violated - parent key not found). myPojo contains collections of other POJOs, which Hibernate tries to save before myPojo, which is not possible.

I understand that setting the flushMode to COMMIT would be a workaround, but that is probably a bad solution as I suspect there is some other basic design flaw here...

If myPojo.id is null, everything goes fine. Could someone explain this behaviour, I would be very grateful.

cheers,
pj


Top
 Profile  
 
 Post subject: Re: newbie: POJO gets id but is not saved
PostPosted: Wed Dec 07, 2005 12:37 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
tanstaafl wrote:
hello,
I have two basic questions regarding persistent objects:

Question 1:
I create a POJO of type A. After that, I create an arbitrary query.

Code:
A myPojo = new A();
Query q = session.createQuery("from ....");



You have to have more code than that....

Just creating a POJO that has a hibernate mapping will NOT generate an ID. It's not even associated with the Session at this point and therefore session.flush() will have no impact.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: newbie: POJO gets id but is not saved
PostPosted: Wed Dec 07, 2005 12:57 pm 
Newbie

Joined: Tue Oct 18, 2005 8:43 am
Posts: 15
pksiv wrote:
You have to have more code than that....

Just creating a POJO that has a hibernate mapping will NOT generate an ID. It's not even associated with the Session at this point and therefore session.flush() will have no impact.


yes, myPojo does contain more data, but my id is null. When running createQuery(), myPojo gets an sequence-id from the database. Does this not mean that a flush is performed?

From "Hibernate in Action" (p. 160):
"Hibernate flushes occur only at the following times:
    [...]
    Sometimes before a query is executed
    [...]
"

I believe this is what happens in my case, because when I set FlushMode.COMMIT, myPojo.id remains null.

/pj


Top
 Profile  
 
 Post subject: Re: newbie: POJO gets id but is not saved
PostPosted: Wed Dec 07, 2005 1:04 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
If this is REALLY all you are doing...

Code:
A myPojo = new A();
Query q = session.createQuery("from ....");


The Hibernate session knows NOTHING about myPojo so has no reason/way to add an ID for it. Is there any code in your Constructor A() ?

There would have to be some code associating myPojo with the hibernate Session in between the
A myPojo = new A(); and the Query q= session.createQuery("from ....");

or it's definitely not Hibernate setting you're id.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: newbie: POJO gets id but is not saved
PostPosted: Wed Dec 07, 2005 1:14 pm 
Newbie

Joined: Tue Oct 18, 2005 8:43 am
Posts: 15
pksiv wrote:
The Hibernate session knows NOTHING about myPojo so has no reason/way to add an ID for it. Is there any code in your Constructor A() ?


yes, you are right. myPojo contains stuff like

B b = (B)session.load(B.class, id);
myPojo.setB(b);

Is this important? Because my question was why myPojo was not saved when Hibernate appears to do a flush(). Please enlighten me :-)

cheers,
pj


Top
 Profile  
 
 Post subject: Re: newbie: POJO gets id but is not saved
PostPosted: Wed Dec 07, 2005 2:18 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
I can't "enlighten"you without all of the information.
http://www.hibernate.org/ForumMailingli ... AskForHelp

And even assuming the flush does send the changes to the database, you will still need to Commit them for them to be stored permanently. This is a database thing, not a hibernate thing.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 5:24 am 
Newbie

Joined: Tue Oct 18, 2005 8:43 am
Posts: 15
well, it seems like you misunderstood my question, but you eventually answered my first question anyway. A flush() does not save to the database, I need to do a commit() as well (sic!). A very basic thing, of course, which I probably should have been aware of.

Any ideas for question 2?

cheers,
pj


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 3:54 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
tanstaafl wrote:
well, it seems like you misunderstood my question, but you eventually answered my first question anyway. A flush() does not save to the database, I need to do a commit() as well (sic!). A very basic thing, of course, which I probably should have been aware of.

Any ideas for question 2?

cheers,
pj


The order in which child objects are saved is determined by the inverse="true/false" attribute on the collection mapping. So that is probably your problem.

posting your mapping files would help....

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


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