我在休眠时遇到了一些麻烦。我想在 CollaborationableImpl 对象中存储事务对象列表。
所以我的 CollaborationableImpl 的休眠配置文件看起来像这样:
<class name="CollaborateableImpl" table="Collaborateable">
<id name="id" type="int" column="id">
<generator class="increment" />
</id>
<property name="name" column="name" type="string" not-null="true" />
<property name="keywords" column="keywords" type="string"/>
<!-- Transactions -->
<list name="transactions" table="Transaction" lazy="false" cascade="all" fetch="select">
<key>
<column name="Collaborateable_id" />
</key>
<index column="idx"/>
<one-to-many class="TransactionImpl" />
</list>
</class>
我的事务配置文件:
<class name="TransactionImpl" table="Transaction">
<id name="id" type="string" column="id">
<!-- Generator is not needed, since the id is generated with uuid on client -->
</id>
<map name="vectorClock" table="VectorClockEntry" cascade="delete">
<key column="Transaction_id" />
<map-key column="User_id" type="int" />
<element column="value" type="long" />
</map>
</class>
所以我创建了一个 CollaborationableImpl 对象,用 session.save(collaborationableImpl) 存储这个对象。一切正常。所以现在我想将一个 TransactionImpl 对象添加到 collaborationImpl 对象的 Transaction 列表中。
TransactionImpl t = new TransactionImpl();
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
collaborateableImpl.addTransaction(t);
session.update(c);
tx.commit();
session.flush();
session.close();
不幸的是,抛出了一个异常:java.sql.SQLException:字段"Collaborateable_id"没有默认值
我明白,冬眠想说什么,但我不知道如何解决这个问题。有什么建议吗?
我找到了解决方案,我需要在事务配置文件中定义一个标签:
<many-to-one name="collaborateable" class="CollaborateableImpl" fetch="select">
<column name="Collaborateable_id" not-null="true" />
</many-to-one>