-->
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.  [ 4 posts ] 
Author Message
 Post subject: Not able to insert the value in foreign key column
PostPosted: Thu May 12, 2011 1:17 am 
Newbie

Joined: Tue May 10, 2011 8:22 am
Posts: 4
Hello everyone I am new at Hibernate.
I am trying one to many relationship in State and City Table
While trying to insert values in a City table with the code
void insertCity()
{
City ct=new City();
Session session=null;
ct.setCityName("GANDHINAGAR");
ct.setStateName("GUJRAT");

try{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
}
catch(Exception e){
System.out.println("Error"+e);
}
finally{
session.save(ct);
System.out.println("One City Inserted Successfully!!!");
session.flush();
Transaction tx = session.beginTransaction();
tx.commit();
}
}

The value of City GANDHINAGAR is inserted but instead of GUJRAT , null is inserted in the City table.....
Please give the solution.........


Top
 Profile  
 
 Post subject: Re: Not able to insert the value in foreign key column
PostPosted: Thu May 12, 2011 1:35 am 
Newbie

Joined: Thu May 12, 2011 1:27 am
Posts: 2
What is your hbm file mapping for both State and City table.


Top
 Profile  
 
 Post subject: Re: Not able to insert the value in foreign key column
PostPosted: Thu May 12, 2011 1:42 am 
Newbie

Joined: Tue May 10, 2011 8:22 am
Posts: 4
My hbm files are as follows:
---------------------------------
State.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="com.dataObject.State" table="STATE">
<id name="StateName" type="java.lang.String">
<column name="STATE_NAME"></column>
</id>
<property name="Capital">
<column name="CAPITAL"></column>
</property>
<bag name="cityList" cascade="all">
<key column="STATE_NAME" />
<one-to-many class="com.dataObject.City" />
</bag>

</class>
</hibernate-mapping>

------------------------------------------------
City.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 package="com.dataObject">
<class name="City" table="CITY">
<id name="CityName" type="java.lang.String">
<column name="CITY_NAME"></column>
</id>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Not able to insert the value in foreign key column
PostPosted: Thu May 12, 2011 1:49 am 
Newbie

Joined: Tue May 10, 2011 8:22 am
Posts: 4
Got the problem....

must have included the property


<property name="StateName" type="java.lang.String">
<column name="STATE_NAME"></column>
</property>


in City.hbm.xml




anyways thanks.


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