声明的元模型属性工作正常BUT继承元模型属性是NULL。为什么? [英] Declared metamodel attributes work fine BUT Inherited Metamodel Attributes are NULL. Why?
问题描述
我无法执行以下测试: -
@Test
public void test(){
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction()。begin();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery< Project> query = builder.createQuery(Project.class);
根< Project> project = query.from(Project.class);
路径< String> name = project.get(Project_.name);
Assert.assertNotNull(name);
路径< EntityLifeCycleImpl> lifeCycle = project.get(Project_.lifeCycle); //问题在这里,抛出NullPointer
Assert.assertNotNull(lifeCycle);
}
它会在 project.get(Project_)中抛出NullPointerException。 lifeCycle)
行。为什么?
java.lang.NullPointerException $ b $ org.hibernate.ejb.criteria.path.AbstractPathImpl.get( AbstractPathImpl.java:138)
PersistenceEntityBase.java
import org.hibernate.annotations.GenericGenerator;
@MappedSuperclass
@Access(AccessType.PROPERTY)
public abstract class PersistentEntityBase {
protected String identifier;
protected EntityLifeCycleImpl lifeCycle = new EntityLifeCycleImpl();
保护PersistentEntityBase(){
}
@Id
@GeneratedValue(generator =generator)
@GenericGenerator(name = generator,strategy =guid,parameters = {})
@Column(name =identifier)
public String getIdentifier(){
return identifier;
}
public void setIdentifier(String identifier){
this.identifier = identifier;
}
@Embedded
public EntityLifeCycleImpl getLifeCycle(){
return lifeCycle;
}
public void setLifeCycle(EntityLifeCycleImpl lifeCycle){
this.lifeCycle = lifeCycle;
}
}
Project.java
@Entity
@Table(name =project)
@Access(AccessType.PROPERTY)
public class Project extends PersistentEntityBase {
private String name;
protected Project(){
}
public Project(String name){
this();
this.name = name;
@Column(name =name,nullable = false,unique = true)
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
EntityLifeCycleImpl.java
@Embeddable
public class EntityLifeCycleImpl implements EntityLifeCycle {
私人字符串createdBy;
私人日期createdDate;
@Column(name =created_by)
public String getCreatedBy(){
return createdBy;
}
public void setCreatedBy(String createdBy){
this.createdBy = createdBy;
@Column(name =created_date)
public Date getCreatedDate(){
return createdDate;
}
public void setCreatedDate(Date createdDate){
this.createdDate = createdDate;
}
}
PersistentEntityBase_.java (使用Hibernate Metamodel Generator生成)
@StaticMetamodel(PersistentEntityBase.class)
public abstract class PersistentEntityBase_ {
public static volatile SingularAttribute< PersistentEntityBase,EntityLifeCycleImpl>生命周期;
public static volatile SingularAttribute< PersistentEntityBase,String>标识符;
Project_.java
@StaticMetamodel(Project.class)
public abstract class Project_ extends PersistentEntityBase_ {
public static volatile SingularAttribute< Project,String>名称;
}
EntityLifeCycleImpl_.java
@StaticMetamodel(EntityLifeCycleImpl.class)
public abstract class EntityLifeCycleImpl_ {
public static volatile SingularAttribute< EntityLifeCycleImpl,String>由...制作;
public static volatile SingularAttribute< EntityLifeCycleImpl,Date>创建日期;
persistence.xml (仅限relavent部分)
< class> com.comp.timetracking.entity.PersistentEntityBase< / class>
< class> com.comp.timetracking.entity.Project< / class>
编辑:
我使用hibernate-entitymanager.3.6。 0.Final和hibernate-jpamodelgen.1.0.0.Final。
编辑2
@Pascal
我认为Hibernate EM 3.6.0.Final允许我们在 @Entity
级别定义 @Embedded
注释字段,但是它在 @MappedSuperclass
级别拒绝这样的字段。你说什么?
因为我在这里看不到文件上传选项,所以我在我的esnips帐户中上传了TestCase。 下载基于maven的 - 项目并运行 SingularAttributeTest.java
。并检出控制台输出
ERROR main metamodel.MetadataContext:413 - 无法找到静态元模型字段:timetracking.entity.Employee_# lifeCycle
点击下载embedded-singular-attribute.zip 链接无需安装下载管理器即可下载文件。 (如果你点击使用绿色箭头下载链接
,你必须安装下载管理器!!)
我使用EclipseLink测试了您的类和测试用例(我删除了Hibernate特定的部分)并通过了测试。但是,Hibernate 3.5.6确实失败了。看起来像Hibernate中的一个bug。顺便说一下,在<$ c中缺少一个 Temporal
注释$ c $>可嵌入
@Column(name =created_date)
@Temporal (TemporalType.DATE)
public Date getCreatedDate(){
return createdDate;
}
你不应该声明 PersistentEntityBase
在 persistence.xml
中(映射的超类不是实体)。
I am not able to run the following test:-
@Test
public void test() {
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Project> query = builder.createQuery(Project.class);
Root<Project> project = query.from(Project.class);
Path<String> name = project.get(Project_.name);
Assert.assertNotNull(name);
Path<EntityLifeCycleImpl> lifeCycle = project.get(Project_.lifeCycle); // problem is here, throws NullPointer
Assert.assertNotNull(lifeCycle);
}
it throws NullPointerException at project.get(Project_.lifeCycle)
line. Why?
java.lang.NullPointerException
at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:138)
PersistenceEntityBase.java
import org.hibernate.annotations.GenericGenerator;
@MappedSuperclass
@Access(AccessType.PROPERTY)
public abstract class PersistentEntityBase {
protected String identifier;
protected EntityLifeCycleImpl lifeCycle = new EntityLifeCycleImpl();
protected PersistentEntityBase() {
}
@Id
@GeneratedValue(generator="generator")
@GenericGenerator(name="generator", strategy="guid", parameters = {})
@Column(name="identifier")
public String getIdentifier() {
return identifier;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
@Embedded
public EntityLifeCycleImpl getLifeCycle() {
return lifeCycle;
}
public void setLifeCycle(EntityLifeCycleImpl lifeCycle) {
this.lifeCycle = lifeCycle;
}
}
Project.java
@Entity
@Table(name="project")
@Access(AccessType.PROPERTY)
public class Project extends PersistentEntityBase {
private String name;
protected Project() {
}
public Project(String name) {
this();
this.name = name;
}
@Column(name="name", nullable=false, unique=true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
EntityLifeCycleImpl.java
@Embeddable
public class EntityLifeCycleImpl implements EntityLifeCycle {
private String createdBy;
private Date createdDate;
@Column(name="created_by")
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
@Column(name="created_date")
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
}
PersistentEntityBase_.java (Generated using Hibernate Metamodel Generator)
@StaticMetamodel(PersistentEntityBase.class)
public abstract class PersistentEntityBase_ {
public static volatile SingularAttribute<PersistentEntityBase, EntityLifeCycleImpl> lifeCycle;
public static volatile SingularAttribute<PersistentEntityBase, String> identifier;
}
Project_.java
@StaticMetamodel(Project.class)
public abstract class Project_ extends PersistentEntityBase_ {
public static volatile SingularAttribute<Project, String> name;
}
EntityLifeCycleImpl_.java
@StaticMetamodel(EntityLifeCycleImpl.class)
public abstract class EntityLifeCycleImpl_ {
public static volatile SingularAttribute<EntityLifeCycleImpl, String> createdBy;
public static volatile SingularAttribute<EntityLifeCycleImpl, Date> createdDate;
}
persistence.xml (only relavent part)
<class>com.comp.timetracking.entity.PersistentEntityBase</class>
<class>com.comp.timetracking.entity.Project</class>
EDIT: I use hibernate-entitymanager.3.6.0.Final and hibernate-jpamodelgen.1.0.0.Final.
EDIT 2
@Pascal
I think Hibernate EM 3.6.0.Final allows us to define @Embedded
annotated field at @Entity
level but it denies such field at @MappedSuperclass
level. What do you say?
As I cannot see "file upload option" here, I've uploaded the TestCase in my esnips account. Download the maven-based-project and run SingularAttributeTest.java
. And checkout the console output
ERROR main metamodel.MetadataContext:413 - Unable to locate static metamodel field : timetracking.entity.Employee_#lifeCycle
Click on "Download embedded-singular-attribute.zip" link to download the file WITHOUT installing the download manager. (If you click on Download link with Green arrow
, you'll have to install the download manager!!)
I tested your classes and test case with EclipseLink (I removed the Hibernate specific parts) and the test passes. But it indeed fails with Hibernate 3.5.6. Looks like a bug in Hibernate.
By the way, you are missing a Temporal
annotation in your Embeddable
@Column(name = "created_date")
@Temporal(TemporalType.DATE)
public Date getCreatedDate() {
return createdDate;
}
And you are not supposed to declare PersistentEntityBase
in the persistence.xml
(a mapped super class is not an entity).
这篇关于声明的元模型属性工作正常BUT继承元模型属性是NULL。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!