class Student
public class Student {
private Long id;
private String name;
private String className;
private List<Phone> phones;
// getter setter
}
class Phone
public class Phone {
private Long id;
private String number;
//getter setter
}
-> mappping file Student.hbm.xml
<id name="id" type="long" column="id">
<generator class="native" />
</id>
<property name="name" column="name" type="string" />
<property name="className" column="class_name" type="string" />
<list name="phones" cascade="all-delete-orphan">
<key column="student_id"/>
<list-index column="idx" />
<one-to-many class="Phone" />
</list>
-> mapping file Phone.hbm.xml
<id name="id" type="long" column="id">
<generator class="native" />
</id>
<property name="number" column="number" type="string" />
when i try to update the the phone number (a list), the previous entry is not deleted but the idx(list index) and the foreign key are null and the new entries are marked with the correct idx and foreign key!!The other data (not a list) is updated completely fine. Here I get the class object from the database, modify that and pass the object to saveorupdate()
, but didn't help. Been trying this so long.
Code to read and update:
private void readAndUpdateStudent() {
Student student = null;
Session session = HibernateUtil.getSessionFactory().openSession();
String name = "John";
try {
String queryString = "from Student student where student.name =:name";
Query query = session.createQuery(queryString);
query.setString("name", name);
student = (Student) query.uniqueResult();
System.out.println(student.getName());
student.setName("Mary");
List<Phone> phones = new ArrayList<Phone>();
phones.add(new Phone("555555")); //here if I SET rather adding, the already existing values, update is done accordingly
phones.add(new Phone("789789"));//but when I use the list as a string type(instead of Phone Type), adding a new 0bject deletes the previous
student.setPhones(phones);//entries and adds new list on its own but not in the case of user defined type.
} catch (Exception e) {
e.printStackTrace();
if (session != null) {
session.close();
}
}finally{
session.close();
updateStudent(student);
}
}
private void updateStudent(Student student) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.saveOrUpdate(student);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
I notice this is a bit old now, but FWIW, the other answer is right, you shouldn't ever set a new collection on a persistent entity retrieved by hibernate as this will create orphans as it has in your case.
Hibernate proxies the entity so it can manage updates to the database (what fields have changed etc) for the entity's properties. So when you retrieve the entity, and set a new list, hibernate looses the ability to track changes for what used to be in that list. Therefore you should update the existing List as was pointed out, so orphans get removed on update. You can also use clear().
List<Phone> phones = student.getPhones();
phones.clear();
phones.add(new Phone("555555"));
phones.add(new Phone("789789"));
session.saveOrUpdate(student);
If you want to avoid duplicates try a Set rather than a list, and implement hashCode() and equals() on the Phone class, for example, it might make sense to check check equality on phone.number.
Hope this helps :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With