In my database table Attribute, I will have a list of data loaded first. Every time, when I want to persist a new record of MyAttribute, I will need to search through the table Attribute first and select the appropriate record from table Attribute before I insert to table 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;
}
}
I'm not sure why I keep on receiving error like:
javax.persistence.PersistenceException: [PersistenceUnit: MyPu] Unable to build EntityManagerFactory
Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
Caused by: org.hibernate.InstantiationException: could not instantiate test object
P.S. This is not the real code that I'm working on, but the structure is more or less the same.