This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with many to one Mapping

 
Vijay Bhaskar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

People.java
------------
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; }

}

Address.java
------------
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");
System.out.println(b_sAddressId);

}



private String createAndStorePeople(String First, String Last) {

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

People l_sobjPeople=new People();
l_sobjPeople.setFirstName(First);
l_sobjPeople.setLastName(Last);

session.save(l_sobjPeople);

session.getTransaction().commit();




return l_sobjPeople.getPeopleID();
}

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

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

l_sobjAddress.setStreet(Street);
l_sobjAddress.setCity(City);
l_sobjAddress.setPeopleID(People_ID);
session.save(l_sobjAddress);

session.getTransaction().commit();

return l_sobjAddress.getAddressId();
}


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

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

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



</class>
</hibernate-mapping>


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

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

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

</class>
</hibernate-mapping>

hibernate.cfg.xml
------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc racle:thin:@10.148.84.102:1524:fmo</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="hbm2ddl.auto">create</property>

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

</session-factory>

</hibernate-configuration>


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
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic