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

发布于 2024-09-30 10:14:52 字数 4772 浏览 0 评论 0原文

我无法运行以下测试:-

@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 元模型生成器生成)

   @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 @帕斯卡
我认为 Hibernate EM 3.6.0.Final 允许我们在 @Entity 级别定义 @Embedded 带注释的字段,但它在 @MappedSuperclass 拒绝这样的字段等级。你怎么说?

由于我在这里看不到“文件上传选项”,因此我已将测试用例上传到我的 esnips 帐户中。 下载基于maven的-项目并运行 SingularAttributeTest.java。并检查控制台输出

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

单击“下载嵌入-singular-attribute.zip”链接即可下载文件,而无需安装下载管理器。 (如果您点击带绿色箭头的下载链接,则必须安装下载管理器!!)

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!!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱冒险 2024-10-07 10:14:52

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

顺便说一句,您在 Embeddable 中缺少 Temporal 注释

@Column(name = "created_date")
@Temporal(TemporalType.DATE)
public Date getCreatedDate() {
    return createdDate;
}

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

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).

一人独醉 2024-10-07 10:14:52

这不是问题。

来自 Hibernate 文档:

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

。 “https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/metamodel.html”rel =“nofollow”>https://docs.jboss.org/hibernate/entitymanager/3.5/reference /en/html/metamodel.html

this is not an issue.

From Hibernate documentation:

If class X extends another class S, where S is the most derived managed class (i.e., entity or mapped superclass) extended by X, then class X_ must extend class S_, where S_ is the metamodel class created for S.

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

你在我安 2024-10-07 10:14:52

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

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

I had the same problem: During deployment the jboss 7.0.2 reported the following error:
"Unable to locate static metamodel field X"
where X is a field inherited from a @MappedSuperclass and annotated with @Embedded (the type of X is a class annotated with @Embeddable).

I solved it adding the classes annotated with @Embeddable and @MappedSuperclass, together with the classes annotated with @Entity, in the list of classes to be managed by the persistence unit. This is configured in the "persistence.xml" file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文