由於性能方面的原因,我打算將hibernate中的許多值類型集映射到一個表。 首先,我確實把它們都放在了自己的表格中,這導致了太多的連接。如何將許多值類型集合映射到Hibernate中的單個表?
我有一個包含了一堆套許多組件類別類,然後我通過這些子類使用實體名稱和鑑別映射到一個表。 請參閱下面的映射片段。
這可以正常工作,但由於包含所有集合的單個表,當組件被移除時很難自動刪除集合。這是因爲它被建模爲一個實體,而Hibernate不能表示該集合已被刪除。
映射的詳細信息:
<class name="com.sample.CategoriesDefault" table="dec_cats" >
<id name="id" column="id" type="string" length="40" access="property">
<generator class="assigned" />
</id>
<component name="incomeInfoMember" class="com.sample.IncomeInfoDefault">
<property name="hasWage" type="boolean" column="inMemWage"/>
...
<component name="wage" class="com.sample.impl.WageDefault">
<property name="hasEmployerWage" type="boolean" column="inMemEmpWage"/>
...
<set name="employerWages" cascade="all-delete-orphan" lazy="false">
<key column="idCats" not-null="true" />
<one-to-many entity-name="mIWaEmp"/>
</set>
</component>
</component>
</class>
<class name="com.sample.NameValueTypeEntityMarker" table="cats_name_amount">
<id name="id" column="id" type="string" length="40" access="property">
<generator class="assigned" />
</id>
<discriminator type="string" force="true">
<column name="objType" index="ixDiscrCatsNameAmt" length="25" not-null="true" />
</discriminator>
<version name="version" column="objVrs" unsaved-value="negative"/>
<property name="name" type="string" column="name" not-null="true" />
<subclass name="com.sample.NameAmountsIntValueTypeEntityMarker">
<property name="amount1" type="integer" column="amount1" access="property" not-null="true"/>
...
<subclass entity-name="mIWaEmp" name="com.sample.EmployerWageDefault" discriminator-value="mIWaEmp" />
<subclass entity-name="pIWaEmp" name="com.sample.impl.EmployerWageDefault" discriminator-value="pIWaEmp" />
</subclass>
</class>
用法:發生
Categories cats = new CategoriesDefault();
Wage wage = new Wage();
Set<EmployerWage> set = new HashSet<EmployerWage>();
set.add(new EmployerWageDefault(cats));
wage.setEmployerWages(set);
IncomeMember inc = new IncomeMemberDefault();
inc.setWage(wage);
cats.setIncomeMember(inc);
cats.saveOrUpdate(); // will store it in the db.
// now remove the income:
cats.setIncomeMember(null);
cats.saveOrUpdate();
cats = getPersister().findCats(cats.getId());
// ERROR: cats still contains IncomeMember, Wage and EmployerWages.
這個錯誤,因爲他們被標記爲分類的兒童的EmployerWages不會自動刪除(見鍵列)等它們是作爲孤兒取出時類別被刪除,而不是他的組件被移除時... 但是,我不能讓EmployerWages工資的孩子作爲工資是一個組件和Hibernate的抱怨,然後...
Pffff ...很難解決這個問題。想法的更多,然後歡迎...
我不明白這與問題有關嗎? – edbras 2014-07-09 13:46:49