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