Hibernate的rollback不起作用?
单向一对一外键关联
<hibernate-mapping package="com.example.hibernate"> <class name="IdCard" table="t_idcard"> <id name="id"> <generator class="native" /> </id> <property name="cardNo" unique="true"/> </class></hibernate-mapping>
<hibernate-mapping package="com.example.hibernate"> <class name="Person" table="t_person"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <many-to-one name="idCard" unique="true"/> </class></hibernate-mapping>
Session session = null; Transaction tx = null; try { session = HibernateUtils.getSession(); tx = session.beginTransaction(); IdCard idCard = new IdCard(); idCard.setCardNo("78383"); Person person = new Person(); person.setName("tom"); person.setIdCard(idCard); session.save(person); tx.commit(); } catch(Exception e) { e.printStackTrace(); tx.rollback(); } finally { HibernateUtils.closeSession(session); }