我无法运行以下测试:-
@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);
}
它在行抛出 NullPointerException project.get(Project_.lifeCycle)
。为什么?
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;
}
}
项目.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;
}
项目_.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。下载基于 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
,你必须安装下载管理器!!)