2013-01-17 34 views
1

我试图添加一个持久化的新添加的类。当我开始时,有一个名为Tournament的类,其类型为Schedule,它简单地包装了一个Session对象的列表。这是该类的hibernate映射。这是id和其他字段已被省略。休眠 - 多次映射一个类

<hibernate-mapping default-access="field"> 
    <class name="fully.qualified.class.Tournament" table="tournaments"> 
    <component name="schedule" class="fully.qualified.class.Schedule"> 
     <list name="sessions" cascade="all-delete-orphan" lazy="true" table="tournament_sessions" > 
     <key column="tournament_id" not-null="true"/> 
     <index column="session_index"/> 
     <one-to-many class="fully.qualified.class.Session"/> 
     </list> 
    </component> 
    </class> 
</hibernate-mapping> 

现在我需要创建一个新类OtherThing也有一个Schedule.所以我复制的会话表,并把它称为other_thing_sessions.我还创建了一个Hibernate映射看起来非常类似于上面的一个:

<hibernate-mapping default-access="field"> 
    <class name="fully.qualified.class.OtherThing" table="other_things"> 
    <component name="schedule" class="fully.qualified.class.Schedule"> 
     <list name="sessions" cascade="all-delete-orphan" lazy="true" table="other_thing_sessions" > 
     <key column="other_thing_id" not-null="true"/> 
     <index column="session_index"/> 
     <one-to-many class="fully.qualified.class.Session"/> 
     </list> 
    </component> 
    </class> 
</hibernate-mapping> 

令我惊讶这导致以下错误:

org.hibernate.MappingException: Repeated column in mapping for entity: fully.qualified.class.Session column: session_index (should be mapped with insert="false" update="false") 

好像休眠不喜欢我反复使用这个类,所以我尝试将属性entity-name="OtherThingSession"添加到一对多元素中。现在我有这个错误:

org.hibernate.MappingException: Association references unmapped class: OtherThingSession 

这让我停了一会儿,所以我回去看看我原来的错误。我决定只更改新映射中索引列的名称。这没有得到休眠的投诉,但坚持不起作用。它似乎试图插入会话对象到tournament_sessions表时,当我试图坚持OtherThing.

任何人有任何想法如何解决这个问题?

回答

1

你不能两次映射一个类。 Hibernate无法处理这个问题。正如你所看到的,你会得到奇怪的错误。 (另外,在第一级缓存不正确,当一个类被映射加倍努力,因为Hibernate使用类来决定哪个表缓存实例属于我不得不对uniqueResult得到两个实例(等异常)(效果在表中)认为,只有一个)

你可以做什么:

创建两个“空”的类,它扩展会话,而不会增加任何功能,例如

public class TournamentSession extends Session {} 

public class OtherSession extends Session {} 

然后在一个<one-to-many>属性使用TournamentSession,在对方使用OtherSessionSession本身从不必映射,也不能在Hibernate属性中使用。