声明的元模型属性工作正常但是继承的元模型属性为NULL。为什么?

时间:2010-11-02 11:14:34

标签: hibernate jpa jpa-2.0 hibernate-entitymanager

我无法运行以下测试: -

@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);
}

它会在project.get(Project_.lifeCycle)行抛出NullPointerException。为什么呢?

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 (使用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 (仅限相关部分)

<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允许我们在@Embedded级别定义@Entity带注释的字段,但它拒绝@MappedSuperclass级别的此类字段。你怎么说?

由于我在这里看不到“文件上传选项”,我已经在我的esnips帐户中上传了TestCase。 Download基于maven的项目并运行SingularAttributeTest.java。并检查控制台输出

ERROR main metamodel.MetadataContext:413 - Unable to locate static metamodel field : timetracking.entity.Employee_#lifeCycle

点击“下载embedded-singular-attribute.zip ”链接下载文件,不安装下载管理器。 (如果单击Download link with Green arrow,则必须安装下载管理器!!)

3 个答案:

答案 0 :(得分:6)

我使用EclipseLink测试了您的类和测试用例(我删除了Hibernate特定部分)并且测试通过了。但它确实因Hibernate 3.5.6而失败。看起来像是Hibernate中的一个错误。

顺便说一句,您在Temporal

中缺少Embeddable注释
@Column(name = "created_date")
@Temporal(TemporalType.DATE)
public Date getCreatedDate() {
    return createdDate;
}

你不应该在PersistentEntityBase中声明persistence.xml(映射的超类不是实体)。

答案 1 :(得分:1)

我遇到了同样的问题:在部署期间,jboss 7.0.2报告了以下错误: “无法找到静态元模型字段X” 其中X是从@MappedSuperclass继承并用@Embedded注释的字段(X的类型是用@Embeddable注释的类)。

我解决了在由持久性单元管理的类列表中添加用@Embeddable和@MappedSuperclass注释的类以及用@Entity注释的类。这是在“persistence.xml”文件中配置的。

答案 2 :(得分:1)

这不是问题。

来自Hibernate文档:

  

如果类X扩展了另一个类S,其中S是由X扩展的派生最多的托管类(即实体或映射的超类),则类X_必须扩展类S_,其中S_是为S创建的元模型类。 / p>

https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/metamodel.html