hibernate 的自增长主键是否不能改。
攻程狮… 2009-05-19 09:15:50 Parent.hbm.xml:
<class name="org.echo.hibernate.Parent" table="parent">
<id name="pid">
<column name="pid"></column>
<generator class="native">
</generator>
</id>
<property name="name"></property>
<set name="childs" inverse="true" cascade="all,delete-orphan">
<key column="pid"></key>
<one-to-many class="org.echo.hibernate.Child"/>
</set>
</class>
Child.hbm.xml:
<class name="org.echo.hibernate.Child" table="child">
<id name="cid">
<column name="cid"></column>
<generator class="native">
</generator>
</id>
<many-to-one name="parent" column="pid" not-null="true"></many-to-one>
</class>
public void testUpdateParent(){
Session session = null;
try {
session = HibernateUtil.getSession();
session.beginTransaction();
Parent parent = (Parent)session.load(Parent.class, 26);
parent.setPid(44);
session.save(parent);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtil.closeSession(session);
}
}
我本来是想测一下级联,改动父对象的ID,然后引用的子对象的外键也变。但就报了
org.hibernate.HibernateException:identifier of an instance of org.echo.hibernate.Parent
was altered from 26 to 44
网上找了找也没有发现找到答案。。我的疑问是hibernate 的自增长主键是否不能改。