Hi all,
When I do the below what is does is that it creates a new row in the PHONE_NUMBER table with the new values of the new phone number object which is right but instead of deleting the old values of phone number (because I have used instance.getPhoneNumbers().clear()) it nulllifies the values of the old record.
This is leading to creation of large number of null rows in the Phone_Number table which should have ideally been deleted. Can anyone explain me why this is happening
Hibernate version: Hibernate 3.0.5
Mapping documents: <class name="MyClass" table="MY_CLASS"> <id name="id" column="ID" type="long" unsaved-value="0"> <generator class="native"/> </id> <property name="requestDate" type="timestamp" column="REQUEST_DATE"/> <property name="requester" column="REQUESTER" type="java.lang.String"/> <set name="phoneNumbers" cascade="all" access="field" lazy="false"> <key column="MYCLASS_ID" not-null="false"/> <one-to-many class="PhoneNumber"/> </set> </class>
<class name="PhoneNumber"> table="PHONE_NUMBER"> <id name="id" column="ID" type="long" unsaved-value="0"> <generator class="native"> </generator> </id> <property name="areaCode" type="java.lang.String" column="AREA_CODE"/> <property name="number" type="java.lang.String" column="NUMBER"/> <property name="extension" type="java.lang.String" column="EXTENSION"/> <property name="type" type="int" column="PHONE_TYPE_ID"/> </class>
Code between sessionFactory.openSession() and session.close():
PhoneNumber phoneNumber = new PhoneNumber("610", "964 5235", "");
MyClass instance = (MyClass) s.get(MyClass.class, id); instance.getPhoneNumbers().clear();//since phone number array is a Set [b]instance.setPhoneNumbers(new HashSet(phoneNumber)); HibernateUtil.currentSession().saveOrUpdate(instance);
Name and version of the database you are using:SQL Server 2000
In case anything is not clear please let me know.
Thanks for help,
Ashish Abrol
|