-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Unable to create ManyToOne relationship succesfully
PostPosted: Sun Jun 20, 2010 7:31 am 
Newbie

Joined: Sun Jun 20, 2010 7:14 am
Posts: 2
Hi All,

I'm trying to get started with Hibernate and JPA. I want to create a ManyToOne relationship between two Java objects. I'm not using any configuration XML files. I'm doing this all programmatically.

I'm using the following code for configuring my Hibernate:

Code:
AnnotationConfiguration configuration = new AnnotationConfiguration();
      configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
      configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/product");
      configuration.setProperty("hibernate.connection.username", "root");
      configuration.setProperty("hibernate.connection.password", "test");
      configuration.setProperty("hibernate.connection.dialect", "org.hibernate.dialect.MySQLDialect");
      configuration.setProperty("hibernate.hbm2ddl.auto", "create"); // create tables when not exist
      configuration.addAnnotatedClass(nl.product.core.cms.domain.ChildImpl.class);
      configuration.addAnnotatedClass(nl.product.core.cms.domain.ParentImpl.class);
configuration.buildSessionFactory();


Here's my code:

Parent class:
Code:
@Entity
@Table(name = "parents")
public class ParentImpl implements Parent {

   // The id
   @Id
   @GeneratedValue
   @Column(name = "id")
   private Integer myId;

   // The children
   @JoinTable(name = "children", joinColumns = { @JoinColumn(name = "parent_id") })
   @OneToMany(targetEntity=nl.product.core.cms.domain.ChildImpl.class, mappedBy = "myParent", cascade = {CascadeType.ALL})
   private List<Child> myChildren;

   /**
    * {@inheritDoc}
    */
   public Integer getId() {
      return myId;
   }

   /**
    * {@inheritDoc}
    */
   public void set(Integer id) {
      myId = id;
   }
   
   /**
    * {@inheritDoc}
    */
   public List<Child> getChildren() {
      return myChildren;
   }

   /**
    * {@inheritDoc}
    */
   public void setChildren(List<Child> children) {
      myChildren = children;
   }

}


Then my Child class:

Code:
@Entity
@Table(name = "children")
public class ChildImpl implements Child {

   // The ID for this website
   @Id
   @GeneratedValue
   @Column(name = "id")
   private Integer myId;
   
   // The parent
   @JoinColumn(name = "parent_id", nullable = false)
   @ManyToOne(targetEntity=nl.product.core.cms.domain.ParentImpl.class)
   private Parent myParent;
   
   /**
    * {@inheritDoc}
    */
   public Integer getId() {
      return myId;
   }

   /**
    * {@inheritDoc}
    */
   public void set(Integer id) {
      myId = id;
   }
   
   /**
    * {@inheritDoc}
    */
   public Parent getParent() {
      return myParent;
   }
   
   /**
    * {@inheritDoc}
    */
   public void setParent(Parent parent) {
      myParent= parent;
   }

}


Then I try to create some data:

Code:
Session session = getSessionFactory().openSession();
      Transaction tx = session.beginTransaction();
      Parent parent = new ParentImpl();
      Child child = new ChildImpl();
      List<Child> list = new ArrayList<Child>();
      list.add(child);
      parent.setChildren(list);
                session.persist(parent);
      tx.commit();
      session.close();


When I execute this I get the following error:

Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: nl.product.core.cms.domain.ChildImpl.myParent

It looks like the parent inside the child is empty. When I add the following before persisting:

Code:
child.setParent(parent)


I get the following error:

Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`product/children`, CONSTRAINT `FKD642333853899F2E` FOREIGN KEY (`id`) REFERENCES `children` (`id`))

This looks like a FK referencing to itself?

Can someone help me with this?


Top
 Profile  
 
 Post subject: Re: Unable to create ManyToOne relationship succesfully
PostPosted: Mon Jun 21, 2010 9:03 am 
Newbie

Joined: Sun Jun 20, 2010 7:14 am
Posts: 2
anyone able to assist me?


Top
 Profile  
 
 Post subject: Re: Unable to create ManyToOne relationship succesfully
PostPosted: Mon Jun 21, 2010 9:52 am 
Newbie

Joined: Tue Feb 16, 2010 1:57 pm
Posts: 4
Maybe you need to do something like:


Code:
Session session = getSessionFactory().openSession();
      Transaction tx = session.beginTransaction();
      Parent parent = new ParentImpl();
      Child child = new ChildImpl();
child.setParent(parent);
      List<Child> list = new ArrayList<Child>();
      list.add(child);
      parent.setChildren(list);
                session.persist(parent);
      tx.commit();
      session.close();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.