-->
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: One To Many Association Cascading Problem
PostPosted: Fri Apr 03, 2009 12:08 am 
Newbie

Joined: Fri Mar 20, 2009 4:37 am
Posts: 6
Hi

I'm using Spring 2.5 + Hibernate (JPA). I have two classes Person and Book. Person has a bidirectional one-to-many relation with book.

Person
Code:
@Entity
public class Person implements IModel, Serializable {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;

  private String firstName;
  private String lastName;

  @OneToMany(mappedBy = "owner", cascade={CascadeType.ALL})
  private Set<Book> books = new HashSet<Book>();
  ...


Book
Code:
@Entity
public class Book implements IModel, Serializable {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;

  private String title;
  private String isbn;
  @ManyToOne
  private Person owner;
  ...


The problem is whenever I add the a book to a person and save the person object, the owner property in book is not set even though I've set the cascading option to all e.g:

Code:
Person person = new Person();
Book book = new Book();
person.getBooks().add(book);
personDao.save(person);


The save method creates both person and book record. But then, when I try to get the owner of the book:
Code:
book.owner()
it returns null. Why does it return null when the cascading option should have set both end of the association?

Thanks

Saito


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 03, 2009 1:55 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Cascading has nothing do with setting the associations. You need to manage that yourself. Then, cascading is used to follow those associations and perform actions on the target entities. In other words, you need to call book.setOwner(person). There is an example in the Hibernate documentation: http://www.hibernate.org/hib_docs/v3/re ... bidir.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 03, 2009 2:44 am 
Newbie

Joined: Fri Mar 20, 2009 4:37 am
Posts: 6
Oh I see now. I thought both ends of the association will be set automatically. Maybe I misunderstood the following code from the Java Persistence with Hibernate book

Code:
Item newItem = new Item();
Bid newBid = new Bid();
newItem.addBid(newBid); // Set both sides of the association
entityManager.persist(newItem);


Thanks by the way


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.