Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Probme in Many-to-One mapping

 
Vijay Bhaskar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybidy,
I am facing some mapping problem actually i created two tables by name People and address,In both the "ID" field is common....
and i am not able to populate the "ID" field from people to address
Here i am providing the whole code ..
If any one solve this problem it would be appreciated..
PLEASE MODIFY THE CODE WHERE THE ERROR IS AND TRY TO SEND THT CODE TO ME


package relation;
People
--------
public class People {
String firstName;
String lastName;

long 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 long getPeopleId() { return peopleId; }

public void setPeopleId(long p_sid)
{
peopleId = p_sid;
}

}


Address
--------
package relation;

public class Address {
String Street;
String City;
String AddressId;
long 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(long id) {
this. peopleId = id;
}
public long getPeopleId()
{
return peopleId;
}

}

HibernateUtil
---------------
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();
long p_id =b_objRelation.createAndStorePeople("Ravi","Kumar", 101);
System.out.println("Out from P : " + p_id);

String b_sAddressId=b_objRelation.createAndStoreAdress("RAJAStreet","NDK");
System.out.println(b_sAddressId);

}



private long createAndStorePeople(String First, String Last, long id) {

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

People l_sobjPeople=new People();

l_sobjPeople.setFirstName(First);
l_sobjPeople.setLastName(Last);

l_sobjPeople.setPeopleId(id);
session.save(l_sobjPeople);

session.getTransaction().commit();
return l_sobjPeople.getPeopleId();
}

private String createAndStoreAdress(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(1);

session.save(l_sobjAddress);
System.out.println("in the address.");
session.getTransaction().commit();

return l_sobjAddress.getAddressId();
}
}

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" cascade="all" />

</class>
</hibernate-mapping>
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="peopleId" >
<generator class="increment"/>
</id>

<property name="lastName" column="LAST_NAME" />
<property name="firstName" column="FIRST_NAME" />
</class>
</hibernate-mapping>


hibernate-configuration.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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic