-->
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.  [ 2 posts ] 
Author Message
 Post subject: Many to many question
PostPosted: Wed Mar 30, 2011 10:40 pm 
Newbie

Joined: Wed Mar 30, 2011 10:22 pm
Posts: 10
Hello everybody!
I have basic question. Unfortunately, I couldn't find clear answer.
I was playing with annotations example from "Beginning Hibernate" book.
The model has several classes, two of them have many to many relationship:

something like this:
class Book {
...
@ManyToMany
Set<Author> authors;
}

class Author {
...
@ManyToMany
Set<Book> books;
}

then they create two books, two authors and add authors to books:

Book b1 = new Book();
Book b2 = new Book();

Author a1 = new Author();
Author a2 = new Author();

b1.authors.add(a1);
b1.authors.add(a2);
b2.authors.add(a1);
b2.authors.add(a2);

and finally call
session.save();

Everything is fine in database, if I open new session and get all objects I see each book has two authors and each author has two books.
But right after session.save() the model is inconsistent- each book has two authors but each author has empty set of books.
It looks very strange, after save() operation all objects are controlled by hibernate, I would expect consistent model.
What do I miss?

Thank you,
Andrey


Top
 Profile  
 
 Post subject: Re: Many to many question
PostPosted: Thu Mar 31, 2011 3:41 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
you also need to add the books to author's book-collections. hibernate does not do that for you on save. if you open another session and read the objects from the database the object-model is consistent.

_________________
-----------------
Need advanced help? http://www.viada.eu


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