Hi!...
Well, I think I doing a very stupid error, but I'm not able to find it... I was following the example of Hibernate in Action to create a many-to-many association. This many-to-many will be between a class: Category and a class: Item. To do so, I'm using a third database table that I call: CATEGORY_ITEM...
Everything seems to be ok!... I create a Category and a Item objects... I set to the Category that has an Item, and to the Item that it has a Category (see code below)... I save one of then, and Hibernate stores both Item and Category and updates de CATEGORY_ITEM to make persistent the relation...
Now the problem occurs when I try to read then again from the database, if I read the Item, Hibernate notices the relation between the Item and the Category and reads the Category and makes that relation (if you look at the output you will see that the number of categories that the Item has is equal to 1), but, if I read the Category, Hibernate doesn't add to the category the Item (if you look at the output you will see that the number of items that the category has is equal to 0). I was trying a lot of diferent combinations... but I'm not able to make it work...
Many thanks!
Hibernate version: 2.1.7
Mapping documents:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="es.usc.etse.basesdedatos.practicajava1.model.Category" table="CATEGORIES" proxy="es.usc.etse.basesdedatos.practicajava1.model.Category">
<id name="id" type="long" column="CATEGORY_ID" unsaved-value="-1">
<generator class="increment" />
</id>
<property name="name" column="NAME" type="string" not-null="true" />
<set name="items" cascade="save-update" lazy="true" table="CATEGORY_ITEM" >
<key>
<column name="CATEGORY_ID" not-null="true" />
</key>
<many-to-many class="es.usc.etse.basesdedatos.practicajava1.model.Item" >
<column name="ITEM_ID" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="es.usc.etse.basesdedatos.practicajava1.model.Item" table="ITEMS" proxy="es.usc.etse.basesdedatos.practicajava1.model.Item">
<id name="id" type="long" column="ITEM_ID" unsaved-value="-1">
<generator class="increment" />
</id>
<!-- Name of the item is immutable. -->
<property name="name" type="java.lang.String" column="NAME" not-null="true" update="false" />
<!-- Limit item description to 4000 characters. -->
<property name="description" type="java.lang.String" column="DESCRIPTION" length="4000" not-null="true" />
<property name="initialPrice" column="INITIAL_PRICE" type="long" not-null="true" />
<property name="buyNowPrice" column="BUY_NOW_PRICE" type="long" not-null="true" />
<property name="reservePrice" column="RESERVE_PRICE" type="long" not-null="true" />
<property name="startDate" column="START_DATE" type="java.util.Date" not-null="true" />
<property name="endDate" column="END_DATE" type="java.util.Date" not-null="true" />
<property name="createdDate" column="CREATE_DATE" type="java.util.Date" not-null="true" />
<set name="categories" inverse="true" cascade="save-update" inverse="false" lazy="true" table="CATEGORY_ITEM">
<key>
<column name="ITEM_ID" not-null="true"/>
</key>
<many-to-many class="es.usc.etse.basesdedatos.practicajava1.model.Category">
<column name="CATEGORY_ID" not-null="true"/>
</many-to-many>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session session1 = hu.getSession();
Item item1 = new Item();
item1.setName("Item ");
item1.setDescription("A new item stored into the database");
item1.setBuyNowPrice(1000);
item1.setInitialPrice(1001);
item1.setReservePrice(1002);
Date date1 = new Date();
item1.setEndDate(date1);
item1.setStartDate(date1);
item1.setCreatedDate(date1);
Category category1 = new Category();
category1.setName("categoria 2");
session1.save(category1);
category1.getItems().add(item1);
item1.getCategories().add(category1);
hu.commitSession(session1); // To force Hibernate to delete any cache and reead from the database... it's just testing...
session1 = hu.getSession();
Item item2 = (Item) session1.load(Item.class, item1.getId());
System.out.println("item2.getCategories().size() = " + item2.getCategories().size());
Category category2 = (Category) session1.load(Category.class, category1.getId());
System.out.println("category2.getItems().size() = " + category2.getItems().size());
Full stack trace of any exception that occurs:No exception occours...
Name and version of the database you are using:MySQL 4.0.20
The generated SQL (show_sql=true):Code:
Hibernate: insert into CATEGORIES (NAME, CATEGORY_ID) values (?, ?)
Hibernate: insert into ITEMS (NAME, DESCRIPTION, INITIAL_PRICE, BUY_NOW_PRICE, RESERVE_PRICE, START_DATE, END_DATE, CREATE_DATE, ITEM_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into CATEGORY_ITEM (ITEM_ID, CATEGORY_ID) values (?, ?)
Hibernate: select item0_.ITEM_ID as ITEM_ID0_, item0_.NAME as NAME0_, item0_.DESCRIPTION as DESCRIPT3_0_, item0_.INITIAL_PRICE as INITIAL_4_0_, item0_.BUY_NOW_PRICE as BUY_NOW_5_0_, item0_.RESERVE_PRICE as RESERVE_6_0_, item0_.START_DATE as START_DATE0_, item0_.END_DATE as END_DATE0_, item0_.CREATE_DATE as CREATE_D9_0_ from ITEMS item0_ where item0_.ITEM_ID=?
Hibernate: select categories0_.ITEM_ID as ITEM_ID__, categories0_.CATEGORY_ID as CATEGORY2___ from CATEGORY_ITEM categories0_ where categories0_.ITEM_ID=?
item2.getCategories().size() = 1
Hibernate: select category0_.CATEGORY_ID as CATEGORY1_0_, category0_.NAME as NAME0_ from CATEGORIES category0_ where category0_.CATEGORY_ID=?
category2.getItems().size() = 0
Debug level Hibernate log excerpt: