我正在使用休眠 4.1.9。我有用户,用户有账户列表,账户有交易列表。这是我的 hbm.xml
<?xml version="1.0"?>
<class name="User" table="users">
<id name="userId" column="userid">
<generator class="increment"/>
</id>
<property name="username" column="username" not-null="true"/>
<property name="password" column="password" not-null="true"/>
<property name="registerDate" type="timestamp" column="register_date"/>
<list name="accounts" table="accounts" inverse="true" cascade="all">
<key column="userid" not-null="true"/>
<index column="accountid"/>
<one-to-many class="com.joe.data.Account"/>
</list>
</class>
<class name="Account" table="accounts">
<id name="accountId" column="accountid">
<generator class="increment"/>
</id>
<property name="balance" type="big_decimal" column="balance"/>
<property name="lastModified" type="timestamp" column="last_modified"/>
<list name="txns" table="transactions" inverse="true" cascade="all">
<key column="accountId" not-null="true"/>
<index column="transactionId"/>
<one-to-many class="com.joe.data.Transaction"/>
</list>
<many-to-one name="userId" class="User" column="userid" not-null="true"
unique="true" cascade="all"/>
<many-to-one name="accountType" class="AccountType" column="account_type"
not-null="true" cascade="all" unique="true" />
</class>
<class name="Transaction" table="transactions">
<id name="transactionId" column="transactionid">
<generator class="increment"/>
</id>
<property name="description" column="description"/>
<property name="amount" type="big_decimal" column="amount"/>
<property name="dateAdded" column="date_added"/>
<property name="reoccuring" type="numeric_boolean" column="reoccuring"/>
<many-to-one name="category" class="Category" column="category"
not-null="true" cascade="all" unique="true" />
</class>
<class name="Category" table="categories">
<id name="categoryId" column="categoryid"/>
<property name="categoryName" column="categoryname" not-null="true"/>
</class>
<class name="AccountType" table="account_types">
<id name="accountType" column="account_type"/>
<property name="accountName" column="name"/>
</class>
如果我将 inverse="true" 留在帐户列表中(在用户中),我会收到 ConstraintViolationException,因为用户 ID 没有放入插入查询中。如果我从帐户列表中取出 inverse="true",我会得到 org.hibernate.MappingException: Repeated column in mapping for entity: com.joe.data.Account column: accountid (应该用 insert="false" 映射更新=“假”)
为了澄清小写名称是数据库列名称,驼峰式名称是类变量名称。我知道 Transaction 类还不能很好地工作,但是如果我可以插入 Accounts,我可以做同样的事情来插入 Transactions。
编辑:我在 Account 类上添加了多对一,现在我得到另一个异常,hibernate 抱怨在 com.joe.data.Account 中缺少 userId 的 getter