File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Problem with many to one Mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem with many to one Mapping" Watch "Problem with many to one Mapping" New topic

Problem with many to one Mapping

Vijay Bhaskar

Joined: Apr 10, 2007
Posts: 6
Hi Everyone,
I am facing some problem with my Hibernate Many-one application
I am able to execute the application partially.The problem is tht "I am not able to retrieve the values from the mapping class"
Here i am providing the whole code wht i did till now
package relation;

public class People {
String firstName;
String lastName;

String PeopleID;

public People () { }

public String getLastName() { return lastName; }

public String getFirstName() { return firstName; }

public void setFirstName( String name) {firstName = name; }

public void setLastName( String name) { lastName = name; }

public String getPeopleID() { return PeopleID; }

public void setPeopleID(String p_sid) {PeopleID = p_sid; }

package relation;

public class Address {
String Street;
String City;
String AddressId;
String PeopleID;

public String getCity() {
return City;

public void setCity(String city) {
this. City = city;
public String getAddressId() {
return AddressId;
public void setAddressId(String id) {
this. AddressId = id;
public String getStreet() {
return Street;
public void setStreet(String street) {
this. Street = street;
public void setPeopleID(String id) {
this. PeopleID = id;
public String getPeopleID()
return PeopleID;


HibernateUtil(For creating the session factory)
package util;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println("Session factory ---->"+ sessionFactory);
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);

public static SessionFactory getSessionFactory() {
return sessionFactory;


Relation (Main)
package relation;

import java.util.Date;
import java.util.List;
import org.hibernate.cfg.*;
import org.hibernate.MappingException;
import org.hibernate.*;

import util.HibernateUtil;

public class Relation {
public static void main(String[] args) throws Exception
System.out.println("Start BUT NOT ENDING");

Relation b_objRelation=new Relation();
String b_sPeopleID=b_objRelation.createAndStorePeople("Ravi","Kumar");
String b_sAddressId=b_objRelation.createAndStoreAdress("b_sPeopleID","RAJAStreet","NDK");


private String createAndStorePeople(String First, String Last) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();

People l_sobjPeople=new People();


return l_sobjPeople.getPeopleID();

private String createAndStoreAdress(String People_ID,String Street, String City) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Address l_sobjAddress=new Address();



return l_sobjAddress.getAddressId();

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"" >

<hibernate-mapping package="relation">
<class name="relation.People" table="PEOPLE" >
<id name="PeopleID" column="PEOPLE_ID" >
<generator class="native"/>

<property name="lastName" column="LAST_NAME" />
<property name="firstName" column="FIRST_NAME" />


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"" >

<hibernate-mapping package="relation">
<class name="relation.Address" table="ADDRESSES">
<id name="AddressId" column="ADDRESS_ID">
<generator class="native"/>
<property name="Street" column="STREET" />
<property name="City" column="CITY" />

<many-to-one name="PeopleID" class="relation.People" column="PEOPLE_ID"
cascade="all" />


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"



<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc racle:thin:@</property>
<property name="connection.username">fmouser</property>
<property name="connection.password">fmouser123</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="">create</property>

<mapping resource="relation/People.hbm.xml"/>
<mapping resource="relation/Address.hbm.xml"/>



In the above application i am able to create and insert values in to person table comming to the address i am able to create address table
but i can't insert values in to the address table(Where mapping operation is taking place in address table)

can anyone try to resolve this problem for me
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Just a few pointers.

1. Take advantage of smart defaults. For instance your Id attribute and column. You should have an attribute in your class called just "id", so Hibernate will automatically append the class name to the id, like if it was in a Person object, then Hibernate will map the "id" to the "person_id" column in the database table without you having to map it yourself. Meaning you don't have to tell Hibernate what column to use for the <id> tag.

2. Have your attributes be the type they should be. All of your attributes in your classes are all Strings. Id is definitely not a String.

3. You are mapping only one side of your relationship. So you will only be able to traverse in one direction, and create/save/update/delete in that direction and not reverse. This is not necessarily wrong, as in some cases it should be one directional, and not bi-directional. But if it only is one direction, know which direction that is and don't expect it to work the other direction.

Good Luck


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Problem with many to one Mapping
jQuery in Action, 3rd edition