JPA 2 XML Mapping of Embedded so that it worked with Hibernate Meta model generator
I want to use JPA 2 Meta model generator for Hibernate (Version 1.1.1-Final) (in a Spring Application). Because I use a Mapped Superclass which is the base for all Entities, and this class is located in an different jar (for reuse) I need to map this class explicit in XML (only for the meta model generation, because it works without any additional stuff on time) --- May somebody has a hint how to solve this in general, but it is not the question.
This mapped Super class (BusinessEntity) uses an Embedded class (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;
...
}
But I do not get the mapping working together with the Generator:
If I use this 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 n开发者_如何学Came="businessId"/>
</attributes>
</mapped-superclass>
<embeddable class="BusinessId"
access="FIELD">
<attributes>
<basic name="businessId">
<column nullable="false" unique="true"/>
</basic>
</attributes>
</embeddable>
</entity-mappings>
The Generator create this two Files:
@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;
}
You can see, the embedded field businessId in BuinessEntity_ is missing!
When I replace the <embedded name="businessId"/>
by <basic name="businessId" />
the Generator create this uncompilable class (The Generic T
can not been resolved).
@StaticMetamodel(BusinessEntity.class)
public abstract class BusinessEntity_ {
public static volatile SingularAttribute<BusinessEntity, Long> id;
public static volatile
SingularAttribute<BusinessEntity, BusinessId<T>> businessId;
}
So my Question is how to map the stuff correct? -- Or is there a better way in general?
<basic> cannot be used for complex types. You must use <embedded>. I have also experienced shortcomings in the annotation processor when applied to orm.xml.
I have submitted this issue a while back (February 18th):
http://opensource.atlassian.com/projects/hibernate/browse/METAGEN-57
Which has not been looked at yet. It seems that it isn't given that much attention as the processor is working for annotations.
I suggest that you submit an issue for your problems as well.
精彩评论