Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!

Vijay Bhaskar

Greenhorn
+ Follow
since Apr 10, 2007
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Vijay Bhaskar

My task is to insert the data through HibernateQueryLanguage thats why i requested for you people to help me.Can you please do that for me
HI Everybody,
Can any one of you help me,How to insert values in HQL Insert
Here i am providing sample code...and i am hard coding the values and insert the values in the table.Can anyone please change my code
so tht i can insert the values through HQL Insert(Provide the HQL in my code)




Thanks & Regards,
Bhaskar

[Edited to use code tags - Paul Sturrock ]
[ June 19, 2007: Message edited by: Paul Sturrock ]
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>
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
Can any one of you can provide the complete example of Hibernate One to One mapping including the SQL tables also