I am writing a sample java console application that writes data in hibernate. All hibernate configurations has been done. Here is the class structure of the project:
public class DBUser implements Serializable {
private static final long serialVersionUID = 1L;
private int userId;
private String username;
private String createdBy;
private Date createdDate;
private DBUserDetail userDetail;
//Getters, Setters and constructors
}
public class DBUserDetail implements Serializable{
private static final long serialVersionUID = 1L;
private Integer userId;
private DBUser user;
private String compName;
private String compDesc;
private String remark;
private Date listedDate;
//Getters, Setters and constructors
}
As you may see, a DBUser can only have one DBUserDetail, and a DBUserDetail can only one DBUser. Assume that, in one http request, user enter both new DBUser and DBUserDetail data. So I would like to add both information in one transaction. Here is my solution:
public void addComplexTypes()
{
try
{
System.out.println("Hibernate one to one (XML mapping)");
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
transaction = session.beginTransaction();
DBUser user = new DBUser();
user.setUsername("username49"); user.setCreatedBy("createdBy49");
DBUserDetail userDetail = new DBUserDetail();
userDetail.setCompName("GENTING Malaysia");
userDetail.setCompDesc("Best resort in the world");
userDetail.setRemark("Nothing Special");
userDetail.setListedDate(new Date());
user.setUserDetail(userDetail);
userDetail.setUser(user);
session.save(user);
transaction.commit();
System.out.println("Done");
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am getting the error:
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint violated(TAHA_SOZGEN.FK_DBUSERDETAIL) - parent key not found
I deduce that, I have to add DBUser in advance. The code works when first search for DBUser and attach it with the DBUserDetail. I change my code and first I add DBUser and later DBUserDetail. So I revise code as below:
public void addComplexTypes()
{
try
{
System.out.println("Hibernate one to one (XML mapping)");
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
transaction = session.beginTransaction();
DBUser user = this.addDbUser("username49", "createdBy49", session, transaction);
// user.setUsername("username49"); user.setCreatedBy("createdBy49");
if(user == null || user.getUserId() <0)
throw new RuntimeException(" this.addDbUser unsuccessfull");
DBUserDetail userDetail = new DBUserDetail();
userDetail.setCompName("GENTING Malaysia");
userDetail.setCompDesc("Best resort in the world");
userDetail.setRemark("Nothing Special");
userDetail.setListedDate(new Date());
user.setUserDetail(userDetail);
userDetail.setUser(user);
session.save(user);
transaction.commit();
System.out.println("Done");
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public DBUser addDbUser(String userName, String createdBy,Session session ,Transaction transaction)
{
try
{
DBUser user = new DBUser();
int anahtar = Integer.MIN_VALUE;
user.setUsername(userName);
user.setCreatedBy(createdBy);
user.setCreatedDate(new Date());
anahtar = (Integer)session.save(user);
user.setUserId(anahtar);
user.setUserId(anahtar);
return user;
} catch (HibernateException hata) {
transaction.rollback();
// TODO Auto-generated catch block
hata.printStackTrace();
}
return null;
}
I am getting the same error:
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint violated(TAHA_SOZGEN.FK_DBUSERDETAIL) - parent key not found
How can I overcome that problem?