2011-06-01 64 views
7

我想使用JPA 2元模型生成器Hibernate(版本1.1.1最終)(在Spring應用程序中)。因爲我使用了Mapped Superclass,它是所有實體的基礎,並且此類位於不同的jar中(爲了重用),所以我需要以XML格式顯式映射這個類(僅適用於元模型生成,因爲它不工作準時追加的東西)---可能有人提示如何解決這個問題,但這不是問題。JPA 2嵌入式XML映射,使其與Hibernate元模型生成器合作

此映射超類(BusinessEntity)使用嵌入類(BusinessId)。

@SuppressWarnings("serial") 
@MappedSuperclass 
public abstract class BusinessEntity<T extends Serializable> 
         implements Serializable { 
    @Id  
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "id") 
    private Long id; 

    @Embedded 
    private BusinessId<T> businessId; 
... 
} 


@Embeddable 
public class BusinessId<T> implements Serializable { 
    @Column(nullable = false, unique = true, name = "businessId") 
    private long businessId; 
    ... 
} 

但我不明白的映射與發電機一起工作:如果我用這個orm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm 
    http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" 
    version="2.0"> 

    <mapped-superclass class="BusinessEntity" 
      access="FIELD">  
     <attributes>    
      <id name="id"> 
       <column nullable="false"/> 
       <generated-value strategy="AUTO"/>    
      </id> 
      <embedded name="businessId"/> 
     </attributes>  
    </mapped-superclass> 

    <embeddable class="BusinessId" 
      access="FIELD"> 
     <attributes> 
      <basic name="businessId"> 
       <column nullable="false" unique="true"/> 
      </basic> 
     </attributes> 
    </embeddable> 
</entity-mappings> 

發電機創建此兩個文件 :

@StaticMetamodel(BusinessEntity.class) 
public abstract class BusinessEntity_ { 
    public static volatile SingularAttribute<BusinessEntity, Long> id; 
} 

@StaticMetamodel(BusinessId.class) 
public abstract class BusinessId_ { 
    public static volatile SingularAttribute<BusinessId, Long> businessId; 
} 

你可以請參閱BuinessEntity_中的嵌入式字段businessId缺失!

當我更換<embedded name="businessId"/>通過<basic name="businessId" />發生器創建這個不可編譯的類(一般T不能得到解決)。

@StaticMetamodel(BusinessEntity.class) 
public abstract class BusinessEntity_ { 

    public static volatile SingularAttribute<BusinessEntity, Long> id; 
    public static volatile 
      SingularAttribute<BusinessEntity, BusinessId<T>> businessId; 
} 

所以我的問題是如何映射的東西是否正確? - 或者總體上有更好的方法嗎?

回答

2

<基本>不能用於複雜類型。您必須使用<嵌入式>。在應用到orm.xml時,我在註釋處理器中也遇到了缺陷。

我已經提交了這個問題而回(2月18日):

http://opensource.atlassian.com/projects/hibernate/browse/METAGEN-57

尚未看着呢。看起來它並沒有像處理器用於註釋那樣給予太多關注。

我建議你也爲你的問題提交問題。