2011-01-23 68 views
2

由于性能方面的原因,我打算将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 ...很难解决这个问题。想法的更多,然后欢迎...

回答

相关问题