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.  [ 9 posts ] 
Author Message
 Post subject: newbie question
PostPosted: Sat Aug 23, 2008 5:31 pm 
Newbie

Joined: Sat Aug 23, 2008 5:00 pm
Posts: 4
Hello

Im a newbie of hibernate I have huge problem solving this I cant figure it out....maybe is it simple but I have googled and searched in tutorials and followed every step but It doesnt work.
I thought that hibernate should do all the work handling objects and store data into corresponding table in DB

Im using Mysql and

What I want to achive is if I create a "People"-object and adds "Event"s to that "People"'s Set<Event> and then do:

session.save(people);

I want to save all data corresponding that "People" in its table and all infomation in the Set<Event> into the Event table. One People can have many Event

In Mysql I have 2 tables corresponding to People and Event and People's id key is an foreign key in the Event table (id_people) so I can bind people to events

When I compile my code I get an error that says:
"Cannot add or update a child row: a foreign key constraint fails"

My *.java files looks like:

People.java and Event.java

People.java
------------------------------------
Code:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class People {
   
   private int id;
   private String namn;
   private Set<Event> events=new HashSet<Event>();
   
   public Set getEvents() {
      return events;
   }

   public void setEvents(Set events) {
      this.items = events;
   }

   public People(){

   }

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getNamn() {
      return namn;
   }

   public void setNamn(String namn) {
      this.namn = namn;
   }
}

------------------------------------------------

Event.java
-------------------------------------------------

Code:
public class Event {
private int id;
private int id_people;
private String namn;

public Event(){
   
}
public int getId() {
   return id;
}

public void setId(int id) {
   this.id = id;
}

public int getId_people() {
   return id_people;
}

public void setId_people(int id) {
   this.id_people = id;
}

public String getNamn() {
   return namn;
}

public void setNamn(String namn) {
   this.namn = namn;
}

}

-----------------------------------------------

My *.hbm.xml files looks like:

people.hbm.xml
----------------------------------------------
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
   <class name="People" table="people">
      <id name="id" column="id" type="java.lang.Integer" >
         <generator class="increment" />
      </id>
      <property name="namn" column="namn" type="java.lang.String" />
      <set name="events" table="event" cascade="all">
         <key column="id_people" />
         <one-to-many class="Event"  />
      </set>
   </class>
</hibernate-mapping>

--------------------------------------------------

event.hbm.xml
----------------------------------------------------
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
   <class name="Event" table="event" >
      <id name="id" column="id" type="java.lang.Integer" >
         <generator class="increment" />
      </id>
      <property name="id_people" column="id_people" type="java.lang.Integer" />
      <property name="namn" column="namn" type="java.lang.String" />
      
   </class>
</hibernate-mapping>

-------------------------------------------------------

I would be so glad if someone could help me solving/explain what Im doing wrong.

Best Regards /MiMa


Top
 Profile  
 
 Post subject: foreign key
PostPosted: Sun Aug 24, 2008 2:58 am 
Newbie

Joined: Sat Aug 23, 2008 3:29 pm
Posts: 7
Are you saving the People object before Event object? Can I pls see the code which does this?


Top
 Profile  
 
 Post subject: Re: foreign key
PostPosted: Sun Aug 24, 2008 3:27 am 
Newbie

Joined: Sat Aug 23, 2008 5:00 pm
Posts: 4
Hello


matiz333matiz wrote:
Are you saving the People object before Event object? Can I pls see the code which does this?


Here is my TestExample cod:

I thought that Hibernate was mapping everything by it self i.e if my People object has an Event object then when I save my People object then I also save my Event object that is wrapped in my People object....isn't Hibenate supposed to work that way, or have I missunderstood everything?

---------------------------------------------------------------
Code:
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class TestExample {

   
   /**
    * @param args
    */
   /**
    * @param args
    */
   public static void main(String[] args) {

      for(int y=1;y<10;y++){
      Date date=new Date();   
      People p1=new People();
      p1.setNamn("People "+date.toString());
      
      for(int z=1;z<5;z++){
      Event i=new Event();
      i.setNamn("Belongs to People "+date.toString()+" event : "+z);
      ink.getItems().add(i);
      }
      createPeople(ink);
         
      }
      
   }


   private static void createPeople(People i_p) {
      Transaction tx = null;
      Session session = InitSessionFactory.getInstance().getCurrentSession();
      try {
         tx = session.beginTransaction();
         session.save(i_p);
         tx.commit();
      } catch (HibernateException e) {
         e.printStackTrace();
         if (tx != null && tx.isActive())
            tx.rollback();
      }
   }


-------------------------------------------------------------------

Thanks for helpeing me!

/MiMa


Top
 Profile  
 
 Post subject: think, it works like this
PostPosted: Sun Aug 24, 2008 8:50 am 
Newbie

Joined: Sat Aug 23, 2008 3:29 pm
Posts: 7
I am also new to Hibernate. I think it works like this for one-to-many association :-

If you have saved the Child object and then save the parent Object, Hibernate issues an insert for parent and update for child to update the parent id(while saving the parent object)

If you are saving the parent object without inserting the Child before, it issues an insert for parent, insert for Child and then update for the Child. Is the id_people column nullable in the event table?


Top
 Profile  
 
 Post subject: Re: think, it works like this
PostPosted: Sun Aug 24, 2008 10:03 am 
Newbie

Joined: Sat Aug 23, 2008 5:00 pm
Posts: 4
Ok...regarding:

Quote:
Is the id_people column nullable in the event table?


No it cant be null it has always a ref_id to its People -obj.

Regards/MiMa


matiz333matiz wrote:
I am also new to Hibernate. I think it works like this for one-to-many association :-

If you have saved the Child object and then save the parent Object, Hibernate issues an insert for parent and update for child to update the parent id(while saving the parent object)

If you are saving the parent object without inserting the Child before, it issues an insert for parent, insert for Child and then update for the Child. Is the id_people column nullable in the event table?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 24, 2008 10:15 am 
Newbie

Joined: Sat Aug 23, 2008 3:29 pm
Posts: 7
Quote:
No it cant be null it has always a ref_id to its People -obj.


Can u make it nullable and check? When the insert is made in the Child table and before update is executed, it might need to put a null value.

Also how does the sql statements which get generated under the hood look? Have u checked them?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 24, 2008 11:29 am 
Newbie

Joined: Sat Aug 23, 2008 3:29 pm
Posts: 7
Change

<key column="id_people" />

to

<key column="id_people" not-null="true"/>

in people.hbm.xml

and

remove the line

<property name="id_people" column="id_people" type="java.lang.Integer" /> from event.hbm.xml

Can you please try the above? Ignore my earlier suggestion about making the fk nullable

Please let me know, how u get on?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 24, 2008 1:35 pm 
Newbie

Joined: Sat Aug 23, 2008 5:00 pm
Posts: 4
Hello

Its working, im so greatful :-D

I sure need to look at this because I dont really understand why it works after have made thoose changes you suggested. Maybe you have time to explain why I should get rid of

"<property name="id_people" column="id_people" type="java.lang.Integer" /> from event.hbm.xml
"

I thought that was the column that defined the primary key in People column.

Thank you!

Regards/ MiMa

matiz333matiz wrote:
Change

<key column="id_people" />

to

<key column="id_people" not-null="true"/>

in people.hbm.xml

and

remove the line

<property name="id_people" column="id_people" type="java.lang.Integer" /> from event.hbm.xml

Can you please try the above? Ignore my earlier suggestion about making the fk nullable

Please let me know, how u get on?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 24, 2008 3:49 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I think you are seeing the same problem that is explained in this example:
http://www.hibernate.org/hib_docs/v3/re ... child.html

Read this and you will see that there are several possible solutions. Personally, I would prefer to make the association bi-directional.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.