-->
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: Please anybody help me out.....
PostPosted: Thu Dec 13, 2007 2:28 am 
Newbie

Joined: Thu Dec 13, 2007 2:04 am
Posts: 2
Hi ,
I am new to hibernate...

I tried to insert child record after parent record is inserted.

Parent Table:

CREATE TABLE users (
id int(11) NOT NULL auto_increment,
username varchar(50)
password varchar(15)
PRIMARY KEY (id))

Child Table with composite key and forien key:

CREATE TABLE phone_numbers (
user_id int(11),
number_type varchar(50),
phone_number int(15),
PRIMARY KEY (user_id,number_type),
FOREIGN KEY (user_id) REFERENCES users (id)

My parent mapping xml: DBgetset.hbm.xml
<?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="hibernate.DBgetset" table="users">
<id name="Id" column="id" type="long">
<generator class="native"/>
</id>
<property name="UserName" type="java.lang.String" >
<column name="username"></column>
</property>
<property name="Password" type="java.lang.String" >
<column name="password"></column>
</property>
<set name="PhoneNumbers" table="phone_numbers" inverse="false" cascade="all">
<key column="user_id" />
<one-to-many class="hibernate.PhoneNumber" />
</set>

</class>
</hibernate-mapping>

My Child mapping xml: PhoneNumber.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>

<class name="hibernate.PhoneNumber" table="PHONE_NUMBERS" >

<composite-id>
<!--<key-property column="USER_ID" name="userId" type="java.lang.Long" />-->
<key-property column="NUMBER_TYPE" name="numberType" type="java.lang.String"/>
<key-property name="userId" column="user_id" type="long"/>

</composite-id>

<property name="phone" type="java.lang.Long">
<column name="phone_number" precision="22" scale="0" />
</property>
<many-to-one name="parent" class="hibernate.DBgetset" column="id" insert="false" update="false" />

</class>
</hibernate-mapping>

When i run the client file i am getting below error,
ERROR org.hibernate.util.JDBCExceptionReporter[main] - Cannot add or update a child row: a foreign key constraint fails.

I am using jboss4.0 hibernate3,

Pl help me to solve this issue.. i tried many times by midifying the code. i didnt get inserted row.

Thanks in advance.

Radha

_________________
Think Smart .....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 13, 2007 5:42 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
hi
send your client code


Amila


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 13, 2007 10:10 am 
Newbie

Joined: Thu Dec 13, 2007 2:04 am
Posts: 2
Thanks for replying my question,
Here is my client code:

public class simpleClient{

public simpleClient() {
}
public static void main(String args[]){
try{

DBEvent dbe = new DBEvent();
DBgetset dbgs = new DBgetset();
PhoneNumber pn=new PhoneNumber();
pn.setNumberType("mobile");
pn.setPhone(Long.parseLong("111111111"));
pn.setUserId(dbgs.getId());
dbgs.setUserName("hai");
dbgs.setPassword("vasu");
dbgs.getPhoneNumbers().add(pn);
dbe.Insert(dbgs,pn);
}
catch(Exception e){
}
}
}

I have POJO class for each table:
public class PhoneNumber implements Serializable {
public PhoneNumber() {
}

private long userId;
private String numberType = "home";
private long phone = 0;

public String getNumberType() {
return numberType;
}
public void setNumberType(String numberType) {
this.numberType = numberType;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
}
// DBgetset pojo
public class DBgetset implements Serializable {
private long id;
private String uname;
private String password;
private java.util.Set phoneNumbers = new java.util.HashSet();
/** Creates a new instance of my_hibernate */

public long getId() {
return id; }
public void setId(long id) {
this.id = id; }
public String getUserName() {
return uname; }
public void setUserName(String uname) {
this.uname = uname; }
public String getPassword() {
return password; }
public void setPassword(String password) {
this.password = password;
}
public java.util.Set getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(java.util.Set phoneNumbers) {
if(phoneNumbers != null)
this.phoneNumbers = phoneNumbers;
}
}

// My session factory & DBEvents class:


public class DBEvent {
List users;
org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
public void Insert(DBgetset dbgs,PhoneNumber pn) throws Exception{
try{
Session s = sf.openSession();
org.hibernate.Transaction tx = s.beginTransaction();
// s.saveOrUpdate(pn);
s.saveOrUpdate(dbgs);
tx.commit();
if(s != null) {
s.flush();
s.close();
}
} catch (HibernateException e) {
}
}



Thanks in advance.....

_________________
Think Smart .....


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.