-->
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.  [ 7 posts ] 
Author Message
 Post subject: Cascading on persist
PostPosted: Mon Apr 13, 2009 1:01 pm 
Beginner
Beginner

Joined: Wed Aug 11, 2004 3:50 pm
Posts: 26
I think I am misunderstanding a limitation of cascading when persisting an object into the database.

Scenario: An Invoice containing many LineItems.

From the docs I see:

- One to many relationships must have 'inverse' on the many side. Meaning that the Invoice side of the mapping must be set to 'inverse'.

- The inverse side is not cascaded when adding entries into a database.

That seems to imply that I cannot simply session.persist(invoice) and have all the line items added. Instead, it seems to indicate that I must first persist the invoice, then loop through the line items and persist each of them.

Is that correct? I hope not... it seems inefficient. Or perhaps there's another way to accomplish a 1-step persist result?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 1:24 pm 
Newbie

Joined: Sat Mar 28, 2009 5:47 pm
Posts: 14
submit misfire...


Last edited by jackpunt on Mon Apr 13, 2009 1:28 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 1:25 pm 
Newbie

Joined: Sat Mar 28, 2009 5:47 pm
Posts: 14
Quote:
One to many relationships must have 'inverse' on the many side. Meaning that the Invoice side of the mapping must be set to 'inverse'.

Surely, the 'many' side is the LineItem? not the 'Invoice'?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 2:13 pm 
Beginner
Beginner

Joined: Wed Aug 11, 2004 3:50 pm
Posts: 26
Actually... as generated by Hibernate Tools... the many side (and inverse) is the invoice. I presume because one invoice has many line items, while a line item belongs to one and only one invoice.

And because of the inverse... if I persist invoice, the line items are not... I have to persist the invoice, then loop through the line items to persist each one. If I don't loop, the database shows no line items.

... feels unnatural... hence my question.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 4:16 pm 
Newbie

Joined: Sat Mar 28, 2009 5:47 pm
Posts: 14
"as generated by Hibernate Tools"; does this mean reveng?

I use reveng to generate my java code, and the class with the collection is annotated:
Code:
class Invoice ...
@OneToMany(fetch=FetchType.LAZY, mappedBy="invoice")
    public Set<LineItem> getLineItems() {
        return this.lineItems;
}


And the items are:
Code:
class LineItem ...
@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="InvoiceId", nullable=false)
    public Job getInvoice() {
        return this.invoice;
    }


In my [limited] experience, if you add LineItems to the invoice.getLineItems() Set, and then update(invoice) the right thing happens.
Assuming the LineItem you add is correctly created with reference back to the Invoice.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 4:38 pm 
Beginner
Beginner

Joined: Wed Aug 11, 2004 3:50 pm
Posts: 26
Hibernate Tools is a download on the Hibernate site, listed with the Hibernate core, Annotations, etc. It is an Eclipse plugin, that among other things allows generating the hibernate-cfg.xml, HBM.xml files, Java classes and DAOs. I have not looked at reveng.

From the fact that reveng generates annotations and Hibernate Tools does not, I presume that Hibernate Tools is not as up to date as reveng. Perhaps they were in a rush to release a new version of the core and did it without waiting for the corresponding updates across the board?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 4:49 pm 
Newbie

Joined: Sat Mar 28, 2009 5:47 pm
Posts: 14
'reveng' is an ant target that invokes the reverse-engineering tools inside hibernatetools.
I use it direct from ant script. (as an emacs user, i can't quite abide Eclipse...)

still uses hbm2hbmxl and hbm2cfgxml and then hbm2java
I don't recall exactly what triggers the Annotation generation...

Anyway, mostly to reassure you that persisting of the Set should work if configured correctly.

(you might post details of your config/markup/code, others may spot the problem)


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