-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem with cascades for many-to-many association
PostPosted: Wed Jan 12, 2005 11:30 pm 
Newbie

Joined: Tue Sep 14, 2004 5:57 pm
Posts: 6
Location: Sunnyvale
I have two tables (book and category) with a third table serving as a many-to-many assignment. What I'm trying to do is very simple, I want to add one or more categories to a book and then persist it. I can read everything great using the many-to-many mapping below, but when I add to the either the book's set of categories, or the category's set of books and perform an update (or updateOrSave), then no SQL (it would be an insert statement) is issued for this. No SQL at all is issued on the commit() of the Transaction.

Sorry if this is something simple. I've done numerous one-to-many relationships in the past and never had any problems, and I think I've done the mapping for the many-to-many just as indicated in FAQs and other examples I've read. I've searched the forums and Google for this kind of thing, and again what I've done seems to be consistent with examples I've found.


Hibernate version:
2.1
Mapping documents:
<!-- Book -->
<hibernate-mapping>
<class
name="demo.hibernate.Book"
table="BOOK"
>

<id
name="bookId"
type="java.lang.Integer"
column="BOOK_ID"
>
<generator class="sequence">
<param name="sequence">SEQ_BOOK</param>
</generator>
</id>

<property
name="title"
type="java.lang.String"
column="TITLE"
not-null="true"
length="100"
/>

<!-- bi-directional many-to-many association to Category -->
<set
name="categories"
table="BOOK_CATEGORY_ASSGN"
lazy="false"
inverse="true"
cascade="all-delete-orphan"
>
<key column="BOOK_ID" />
<many-to-many
column="CATEGORY_ID"
class="demo.hibernate.Category"
/>
</set>

</class>
</hibernate-mapping>

<!-- Category -->
<hibernate-mapping>
<class
name="demo.hibernate.Category"
table="CATEGORY"
>

<id
name="categoryId"
type="java.lang.Integer"
column="CATEGORY_ID"
>
<generator class="assigned" />
</id>

<property
name="name"
type="java.lang.String"
column="NAME"
not-null="true"
length="100"
/>

<!-- Associations -->

<!-- bi-directional many-to-many association to Book -->
<set
name="books"
table="BOOK_CATEGORY_ASSGN"
lazy="false"
inverse="true"
cascade="all-delete-orphan"
>
<key column="CATEGORY_ID" />
<many-to-many
column="BOOK_ID"
class="demo.hibernate.Book"
/>
</set>

</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Transaction tx = sess.beginTransaction();
Book book = (Book) sess.load(Book.class, new Integer(5000)); // Macbeth
System.out.println("Got book=" + book.getTitle());
Set categories = book.getCategories();
if (categories == null)
categories = new HashSet();
Category cat = (Category) sess.load(Category.class, new Integer(400)); // drama
if (categories.contains(cat)){
System.out.println("Category already set");
}
else{
categories.add(cat);
}
Set books = cat.getBooks();
if (books == null)
books = new HashSet();
books.add(book);
sess.update(book);
tx.commit();

Full stack trace of any exception that occurs:
No exception
Name and version of the database you are using:
Oracle 9i
The generated SQL (show_sql=true):

Hibernate: select book0_.BOOK_ID as BOOK_ID1_, book0_.TITLE as TITLE1_ from LIBRARY.BOOK book0_ where book0_.BOOK_ID=?

Hibernate: select categories0_.BOOK_ID as BOOK_ID__, categories0_.CATEGORY_ID as CATEGORY2___, category1_.CATEGORY_ID as CATEGORY1_0_, category1_.NAME as NAME0_ from LIBRARY.BOOK_CATEGORY_ASSGN categories0_ inner join LIBRARY.CATEGORY category1_ on categories0_.CATEGORY_ID=category1_.CATEGORY_ID where categories0_.BOOK_ID=?

Hibernate: select categories0_.BOOK_ID as BOOK_ID__, categories0_.CATEGORY_ID as CATEGORY2___, category1_.CATEGORY_ID as CATEGORY1_0_, category1_.NAME as NAME0_ from LIBRARY.BOOK_CATEGORY_ASSGN categories0_ inner join LIBRARY.CATEGORY category1_ on categories0_.CATEGORY_ID=category1_.CATEGORY_ID where categories0_.BOOK_ID=?

Hibernate: select categories0_.BOOK_ID as BOOK_ID__, categories0_.CATEGORY_ID as CATEGORY2___, category1_.CATEGORY_ID as CATEGORY1_0_, category1_.NAME as NAME0_ from LIBRARY.BOOK_CATEGORY_ASSGN categories0_ inner join LIBRARY.CATEGORY category1_ on categories0_.CATEGORY_ID=category1_.CATEGORY_ID where categories0_.BOOK_ID=?

Hibernate: select categories0_.BOOK_ID as BOOK_ID__, categories0_.CATEGORY_ID as CATEGORY2___, category1_.CATEGORY_ID as CATEGORY1_0_, category1_.NAME as NAME0_ from LIBRARY.BOOK_CATEGORY_ASSGN categories0_ inner join LIBRARY.CATEGORY category1_ on categories0_.CATEGORY_ID=category1_.CATEGORY_ID where categories0_.BOOK_ID=?

Hibernate: select category0_.CATEGORY_ID as CATEGORY1_0_, category0_.NAME as NAME0_ from LIBRARY.CATEGORY category0_ where category0_.CATEGORY_ID=?

Hibernate: select books0_.CATEGORY_ID as CATEGORY2___, books0_.BOOK_ID as BOOK_ID__, book1_.BOOK_ID as BOOK_ID0_, book1_.TITLE as TITLE0_ from LIBRARY.BOOK_CATEGORY_ASSGN books0_ inner join LIBRARY.BOOK book1_ on books0_.BOOK_ID=book1_.BOOK_ID where books0_.CATEGORY_ID=?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.