Hibernate: updating associated objects

I have two tables say parent and child.

PARENT STRUCTURE

ID || NAME

primary key is ID

CHILD STRUCTURE

ID || NAME || PAREND_ID

primary key is ID

foreign key is PARENT_ID

The parent class is as follows:

class parent {
private String id;
private String name;
//getter and setter methods
}

The child class is as follows

class child {
private String id;
private String name;
private String parent_id;
//getter and setter methods
}

The hibernate mapping for parent is as follows:

<hibernate-mapping>
<class name = "parent" table="parent"></class>
<id.....>....<id>
<property name = "name" column = "NAME"/>
</hibernate-mapping>
<hibernate-mapping>
<class name = "child" table="child"></class>
<id.....>....<id>
<property name = "name" column = "NAME"/>
<property name = "parent_id" column = "PARENT_ID"/>
</hibernate-mapping>

I want that if I change the id of parent, the parent_id column in child table should also get updated with the new value. Also the association is unidirectional from parent to child and one parent can have many child. Can you help me with this. Thanks


Your classes are not associated with each other. There is not much that you can do. Hibernate just does not know that they are related. You need to introduce actual assiciations.

Also, changing primary key of the existing object does not sound as right design decision.

链接地址: http://www.djcxy.com/p/37048.html

上一篇: 休眠:与许多标准

下一篇: 休眠:更新关联的对象