0

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?

tahasozgen
  • 469
  • 5
  • 27
  • Use annotation on the DBuserDetail. `@OneToOne(mappedBy="namehere", cascade=CascadeType.PERSIST)` – Silverfang Jun 18 '20 at 12:02
  • I am using xml mapping, and xml mapping of DBUsreDetail is: – tahasozgen Jun 18 '20 at 12:24
  • I found [this](https://mkyong.com/hibernate/hibernate-cascade-example-save-update-delete-and-delete-orphan/). Maybe this will help I dont know much in xml mapping – Silverfang Jun 18 '20 at 12:28
  • I will inspect the link. I seems I will turn into annotation for Hibernate configuration. – tahasozgen Jun 18 '20 at 12:38
  • @tahasozgen you do not need to switch to annotation based configuration for setting cascade type. you can use `cascade="save-update"` in your entity relationship definition within your xml configuration file. – feanor07 Jun 18 '20 at 13:11

1 Answers1

0

Silverfang's answer is right. I have used xml based mapping, and I have inserted "cascade-save-update" as feanor07 stated, and it does not solve the problem. When I used "@OneToOne(mappedBy="namehere", cascade=CascadeType.PERSIST)" as stated by Silverfang, it works.

tahasozgen
  • 469
  • 5
  • 27