-->
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.  [ 3 posts ] 
Author Message
 Post subject: Link table with additional columns-saving in one transaction
PostPosted: Fri Aug 20, 2010 2:32 am 
Newbie

Joined: Sun Jun 27, 2010 1:45 pm
Posts: 4
Hi,

I am learning Hibernate and just read the chapter "7.2.3 Adding columns to join tables" of the "Java Persistance with Hibernate" book. My goal is to save Item, Category and CategorizedItem in one transaction.

There is a constructor there (page 305):

Code:
public CategorizedItem(String username, Category category, Item item) {
    // Set fields
    this.username = username;

    this.category = category;
    this.item = item;

    // Set identifier values
    this.id.categoryId = category.getId();
    this.id.itemId = item.getId();

    // Guarantee referential integrity
    category.getCategorizedItems().add(this);
    item.getCategorizedItems().add(this);
}


It accepts category and item objects. If I create a Category and an Item and want to connect them with this technique, they obviously have to be persisted BEFORE, as category.getId() and item.getId() return null. Or, of course, the Category and Item must be fetched from database, but in my case, they do not exist yet.

Is there "a trick in the Hibernate bag" that can cascade the saving of a join table? The join table have additional columns. I want to save all three objects in the onSuccess handler in my web page controller. All three entities or none of them must be inserted.

Thanks.


Top
 Profile  
 
 Post subject: Re: Link table with additional columns-saving in one transaction
PostPosted: Fri Aug 20, 2010 2:37 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
yes.

attribute cascade in mapping file full fills your purpose. Please go through hibernate-mapping.dtd

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
 Post subject: Re: Link table with additional columns-saving in one transaction
PostPosted: Fri Aug 20, 2010 4:26 am 
Newbie

Joined: Sun Jun 27, 2010 1:45 pm
Posts: 4
I know, for example, that setting @Cascade will cause transitive persistence, like:

Code:
@Cascade(value = CascadeType.SAVE_UPDATE)
public List<CategorizedItem> getCategorizedItems() {
    return this.categorizedItems;
}


and the same on the other side, but this only works if Item and Category are already in persitent state (they have id).

Or did you mean something else?


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