Я не могу выполнить следующий тест: -
@Test
public void test() {
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery query = builder.createQuery(Project.class);
Root project = query.from(Project.class);
Path name = project.get(Project_.name);
Assert.assertNotNull(name);
Path 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;
}
}
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 lifeCycle;
public static volatile SingularAttribute identifier;
}
Project_.java
@StaticMetamodel(Project.class)
public abstract class Project_ extends PersistentEntityBase_ {
public static volatile SingularAttribute name;
}
EntityLifeCycleImpl_.java
@StaticMetamodel(EntityLifeCycleImpl.class)
public abstract class EntityLifeCycleImpl_ {
public static volatile SingularAttribute createdBy;
public static volatile SingularAttribute createdDate;
}
persistence.xml (только соответствующая часть)
com.comp.timetracking.entity.PersistentEntityBase
com.comp.timetracking.entity.Project
РЕДАКТИРОВАТЬ: Я использую hibernate-entitymanager.3.6.0.Final и hibernate-jpamodelgen.1.0.0.Final.
EDIT 2
@Pascal
Я думаю, что Hibernate EM 3.6.0.Final позволяет нам определять аннотированное поле @Embedded
на уровне @Entity
, но запрещает такое поле на уровне @MappedSuperclass
. Что вы скажете?
Поскольку я не вижу здесь «вариант загрузки файла», я загрузил TestCase в свою учетную запись esnips. Загрузите проект на основе maven и запустите SingularAttributeTest.java
. И проверьте вывод консоли.
ERROR main metamodel.MetadataContext:413 - Unable to locate static metamodel field : timetracking.entity.Employee_#lifeCycle
Щелкните ссылку « Download embedded-singular-attribute.zip », чтобы загрузить файл БЕЗ установки менеджера загрузки. (Если вы нажмете на ссылку загрузки с зеленой стрелкой
, вам придется установить менеджер загрузок !! )