Hi All, I am reposting this as my previous post
http://forum.hibernate.org/viewtopic.php?t=935351 seemed to get truncated.
Is there a size limit for posting to this forum?
In my unit test below something weird is happening. When the test is run it fails at this line
assertFalse("account4 should not contain elist2",account4.getElists().contains(elist2));
However if I comment out the code marked
failure code & run the commented out code marked
successful code the test passes. I don't really understand why it works if I reload my Account Object before removing the EList from the collection, but not when I re-attach the detached Account Object & then remove the EList from the collection. It seems that the
"cascade-all-delete-orphan" works, but only with a reloaded Account Object. I have been working through Hibernate in Action (which incidentally, Christian & Gavin is Awesome!! Thanks!) & have created this code after getting through Chapters 3 & 4, and I thought it should work. If anyone can please explain to me why it doesn't work I'd be really grateful, as it will really aid my understanding.
Please note that I do have a tearDown() method in my test which cleans up by deleting the Account, so the execution of this tearDown() method will be seen in the logs.
Code:
public void testRemoveEList() throws Exception {
//save account
Account account1 = TestConstants.getTestAccount();
EList elist1 = TestConstants.getTestEList("TEST");
PERSISTENCE_MANAGER.saveObject(account1);
//look up account
Account account2 = PERSISTENCE_MANAGER.findAccountByEmailAddress(TestConstants.TEST_EMAIL_ADDRESS);
account2.addEList(elist1);
PERSISTENCE_MANAGER.saveObject(elist1);
assertTrue(account2.getElists().size() == 1);
// successful code
// Account account3 = PERSISTENCE_MANAGER.findByPk(account2.getPk());
// assertTrue("account3 should contain elist1",account3.getElists().contains(elist1));
// EList elist2 = account3.getEListByPk(elist1.getPk());
// assertTrue("the list should contain this object, & should successfully remove it. elist1=" + elist1 +
// " elistFromSet=" + elist2, account3.getElists().remove(elist2));
// assertTrue(account3.getElists().size() == 0);
// PERSISTENCE_MANAGER.saveObject(account3);
// assertFalse("account3 should not contain elistFromSet",account3.getElists().contains(elist2));
// end successful code
//failure code
EList elist2 = account2.getEListByPk(elist1.getPk());
assertTrue("remove elist2",account2.getElists().remove(elist2));
PERSISTENCE_MANAGER.saveObject(account2);
//end failure code will fail on account4 should not contain elist2
Account account4 = PERSISTENCE_MANAGER.findByPk(account1.getPk());
assertFalse("account4 should not contain elist2",account4.getElists().contains(elist2));
EList elist3 = PERSISTENCE_MANAGER.findEListByPk(elist1.getPk());
assertNull("elist3 should be null",elist3);
Account account5 = PERSISTENCE_MANAGER.findAccountByEmailAddress(TestConstants.TEST_EMAIL_ADDRESS);
assertNotNull(account5);
Set elists3 = account5.getElists();
assertTrue("The list size should be 0 but it is " + elists3.size(), elists3.size() == 0);
HibernateEListManager elistManager = new HibernateEListManager();
assertNull("this elist should be null",elistManager.findEListByPk(elist1.getPk()));
}
Hibernate version: 2.1.6
Mapping documents:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.easylists.Account" table="ACCOUNT">
<id name="hibernateId" type="long" column="HIBERNATE_ID">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<version name="version" column="VERSION"/>
<property name="pk" type="long">
<column name="PK" not-null="true"/>
</property>
<property name="active" type="boolean">
<column name="ACTIVE" not-null="true"/>
</property>
<property name="hasBeenActivated" type="boolean">
<column name="HAS_BEEN_ACTIVATED" not-null="true"/>
</property>
<property name="createdDate" type="java.util.Date">
<column name="CREATED_DATE" not-null="true"/>
</property>
<property name="expiryDate" type="java.util.Date">
<column name="EXPIRY_DATE"/>
</property>
<property name="title" type="string">
<meta attribute="use-in-tostring">true</meta>
<column name="TITLE" not-null="true"/>
</property>
<property name="forename" type="string">
<meta attribute="use-in-tostring">true</meta>
<column name="FORENAME" not-null="true"/>
</property>
<property name="surname" type="string">
<column name="SURNAME" not-null="true"/>
</property>
<property name="emailAddress" type="string">
<column name="EMAIL_ADDRESS" not-null="true" unique="true" index="EMAIL_IDX"/>
</property>
<property name="password" type="string">
<column name="PASSWORD" not-null="true"/>
</property>
<property name="quota" type="int">
<column name="QUOTA"/>
</property>
<component name="address" class="com.easylists.Address">
<property name="houseNumber" type="string">
<column name="HOUSE_NUMBER" not-null="true"/>
</property>
<property name="houseName" type="string">
<column name="HOUSE_NAME" not-null="true"/>
</property>
<property name="street" type="string">
<meta attribute="use-in-toString">true</meta>
<column name="STREET" not-null="true"/>
</property>
<property name="townOrCity" type="string">
<column name="TOWN_OR_CITY" not-null="true"/>
</property>
<property name="countyOrState" type="string">
<column name="COUNTY_OR_STATE" not-null="true"/>
</property>
<property name="postcode" type="string">
<column name="POSTCODE" not-null="true"/>
</property>
<property name="country" type="string">
<column name="COUNTRY" not-null="true"/>
</property>
</component>
<set name="elists" table="ELIST" cascade="all-delete-orphan" inverse="true" >
<key column="ACCOUNT_ID"/>
<one-to-many class="com.easylists.EList"/>
</set>
<set name="emailAddressBooks" table="EMAIL_ADDRESS_BOOK" cascade="all-delete-orphan" >
<key column="ACCOUNT_ID"/>
<one-to-many class="com.easylists.EmailAddressBook"/>
</set>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.easylists.EList" table="ELIST">
<id name="hibernateId" type="long" column="HIBERNATE_ID">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<version name="version" column="VERSION"/>
<property name="pk" type="long">
<column name="PK" not-null="true"/>
</property>
<property name="elistName" type="string">
<column name="ELIST_NAME" not-null="true"/>
</property>
<property name="emailMessage" type="string">
<column name="EMAIL_MESSAGE"/>
</property>
<property name="password" type="string">
<column name="PASSWORD"/>
</property>
<property name="hideWhenPurchased" type="boolean">
<column name="HIDE_WHEN_PURCHASED" not-null="true"/>
</property>
<property name="sendEmailWhenItemPurchased" type="boolean">
<column name="EMAIL_WHEN_ITEM_PURCHASED" not-null="true"/>
</property>
<property name="expiryDate" type="java.util.Date">
<column name="EXPIRY_DATE"/>
</property>
<many-to-one name="account" class="com.easylists.Account" column="ACCOUNT_ID" not-null="true"/>
<set name="elistItems" table="ELIST_ITEM" cascade="all-delete-orphan">
<key column="ELIST_ID"/>
<one-to-many class="com.easylists.EListItem"/>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():This code is the PERSISTENCE_MANAGER.saveObject(Object ob) mthod called from my test codeCode:
public static void saveObject(Object ob) throws PersistenceException {
try {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.openSession();
tx = session.beginTransaction();
session.saveOrUpdate(ob);
tx.commit();
}
catch (StaleObjectStateException e) {
if (tx != null) {
tx.rollback();
}
throw new StalePersistentObjectException(e);
}
catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
throw new PersistenceException(e);
}
finally {
closeSession(session);
}
}
catch (HibernateException e) {
throw new PersistenceException(e);
}
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
MySQL 4.0
The generated SQL (show_sql=true):
In order to avoid posting a lot of data twice & because I think there may be an issue with the size of messages that can be posted to this forum please see my earlier post here for the generated SQL
http://forum.hibernate.org/viewtopic.php?t=935351
Debug level Hibernate log excerpt:
In order to avoid posting a lot of data twice & because I think there may be an issue with the size of messages that can be posted to this forum please see my earlier post here for the log excerpt
http://forum.hibernate.org/viewtopic.php?t=935351
[/b]