我想使用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;
}
所以我的問題是如何映射的東西是否正確? - 或者總體上有更好的方法嗎?