【发布时间】:2014-08-30 07:57:21
【问题描述】:
在我的数据库表属性中,我将首先加载一个数据列表。每次我想持久化一条MyAttribute的新记录时,我需要先在表Attribute中搜索,然后从表Attribute中选择合适的记录,然后再插入到表MyAttribute中。
@Entity
class MyAttribute{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne(targetEntity = Attribute.class)
@JoinColumn(name="attribute_id", referencedColumnName="id")
Attribute detail;
private String greet;
public MyAttribute(){
this.greet = "Hello World.";
this.detail = new MyDbLayer().selectAttributeDetail("first"); //Error is thrown here.
}
//getter & setter
}
@Entity
class Attribute{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Index(name = "name_index")
@Column(unique=true )
private String name;
//getter & setter
}
class MyDbLayer{
private EntityManagerFactory emf;
public MyDbLayer() {
emf = Persistence.createEntityManagerFactory("MyPu");
}
public Attribute selectAttributeDetail(String name) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Query queryGetAttribute = em.createQuery("select a from Attribute a where a.name = :attributeName");
List<AttributeDescription> attributeDescList = queryGetAttribute.setParameter("attributeName", name).getResultList();
AttributeDescription tempAttribute = null;
if (!attributeDescList.isEmpty()) {
tempAttribute = (AttributeDescription) attributeDescList.get(0);
}
em.clear();
em.close();
return tempAttribute;
}
}
我不知道为什么我不断收到如下错误:
javax.persistence.PersistenceException: [PersistenceUnit: MyPu] 无法 构建EntityManagerFactory
原因:org.hibernate.MappingException:无法获取构造函数 对于 org.hibernate.persister.entity.SingleTableEntityPersister
原因:org.hibernate.InstantiationException:无法实例化 测试对象
附:这不是我正在处理的真正代码,但结构或多或少是相同的。
【问题讨论】: