日期:2014-05-20  浏览次数:20834 次

Hibernate的unique="true"无效
小弟最近研究hibernate,遇到下面的迷惑,请大虾指教下

<hibernate-mapping>
  <class name="ssj.Person" table="PERSON" schema="SSJ">
  <id name="personid" type="java.lang.Integer">
  <column name="PERSONID" precision="22" scale="0" />
  <generator class="assigned"></generator>
  </id>
  <many-to-one name="addressid" column="ADDRESSID" class="ssj.Address"
  unique="true" not-null="true">
  </many-to-one>
  </class>
</hibernate-mapping>

Test类
Address add=new Address();
Person per1=new Person();
Person per2=new Person();
add.setAddressid(1);
per1.setPersonid(11);
per2.setPersonid(12);
per1.setAddressid(add);
per2.setAddressid(add);
Session se=HibernateSessionFactory.getSession();
Transaction tx=se.beginTransaction();
se.save(add);
se.save(per1);
se.save(per2);
tx.commit();
se.close();

为什么执行能通过?

<many-to-one name="addressid" column="ADDRESSID" class="ssj.Address"
  unique="true" not-null="true">
</many-to-one>
中已经指明了unique="true",为什么不报唯一值约束的错误?


------解决方案--------------------
se.save(add);
se.save(per1);
se.save(per2);

只会处理se.save(per2); 这就是 事物的概念
------解决方案--------------------
Java code
<many-to-one name="addressid" column="ADDRESSID" class="ssj.Address"
  unique="true" not-null="true">
</many-to-one>