Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.1.3
Mapping documents:
<?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>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@161.41.9.87:1521:Scott</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.username">Scott</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.cglib.use_reflection_optimizer">false</property>
<mapping resource="Student.hbm.xml"/>
<mapping resource="Colleage.<?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="ColleagePOJO" table="colleage">
<id name="colleageID" column="colleage_ID">
<generator class="assigned"/>
</id>
<property name="colleageName" column="colleage_NAME" type="string"/>
<set name="students" table="student" inverse="true" cascade="all">
<key column="colleage_ID"/>
<one-to-many class="StudentPOJO"/>
</set>
</class>
</hibernate-mapping>"/>
</session-factory>
</hibernate-configuration>
Student.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="StudentPOJO" table="student">
<id name="studentID" column="student_ID">
<generator class="assigned"/>
</id>
<property name="colleageID" column="colleage_ID" />
<property name="studentName" column="student_NAME" type="string"/>
</class>
</hibernate-mapping>
Colleage.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="ColleagePOJO" table="colleage">
<id name="colleageID" column="colleage_ID">
<generator class="assigned"/>
</id>
<property name="colleageName" column="colleage_NAME" type="string"/>
<set name="students" table="student" inverse="true" cascade="all">
<key column="colleage_ID"/>
<one-to-many class="StudentPOJO"/>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
import java.util.Collection;
import java.util.Iterator;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.taxware.twe.util.hibernate.HibernateUtil;
/**
* @author 123985 TODO To change the template for this generated type comment go
* to Window - Preferences - Java - Code Style - Code Templates
*/
public class ColleageStudent
{
/**
* Constructor
*/
public ColleageStudent()
{
}
public static void main(String args[])
{
Long colleageID = null;
Long studentID = null;
String colleageName;
String studentName;
System.out.println("hai");
Session session = HibernateUtil.getInstance().getCurrentSession();
Transaction tx = session.beginTransaction();
ColleageStudent ex = new ColleageStudent();
try
{
colleageID = new Long(8);
studentID = new Long(8);
colleageName = "SCJC";
studentName = "tiny";
ColleagePOJO colleage = new ColleagePOJO();
colleage = ex.createColleage(colleageID, colleageName, studentID,
studentName);
System.out.println("b4 commit");
colleage.getStudents();
if (colleage.getStudents() == null)
{
System.out.println("null");
}
else
{
System.out.println("not null");
}
tx.commit();
System.out.println("after commit");
Collection studInfo = colleage.getStudents();
if (studInfo == null)
{
System.out.println("null afet commit");
}
else
{
System.out.println(" not null ");
for (Iterator iterator = studInfo.iterator(); iterator.hasNext();)
{
StudentPOJO studentP = (StudentPOJO) iterator.next();
System.out.println(" students name -->" + studentP.getStudentName());
}
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(" Exception");
}
}
public ColleagePOJO createColleage(Long colleageID, String colleageName,
Long studentID, String studentName)
{
ColleagePOJO colleage = new ColleagePOJO();
try
{
colleage.setColleageID(colleageID);
colleage.setColleageName(colleageName);
HibernateUtil.create(colleage);
System.out.println("going to calling student-----");
createStudent(studentID, studentName, colleageID);
}
catch (Exception e)
{
System.out.println("EXCEPTION");
}
return colleage;
}
public void createStudent(Long studentID, String studentName, Long colleageID)
{
try
{
StudentPOJO student = new StudentPOJO();
student.setStudentID(studentID);
student.setStudentName(studentName);
student.setColleageID(colleageID);
HibernateUtil.create(student);
}
catch (Exception e)
{
System.out.println("EXCEPTION");
}
}
}
------- Student POJO ----
public class StudentPOJO
{
private String studentName;
private Long studentID;
private Long colleageID;
/**
* @return Returns the studentID.
*/
public Long getStudentID()
{
return this.studentID;
}
/**
* @param studentID The studentID to set.
*/
public void setStudentID(Long studentID)
{
this.studentID = studentID;
}
/**
* @return Returns the studentName.
*/
public String getStudentName()
{
return this.studentName;
}
/**
* @param studentName The studentName to set.
*/
public void setStudentName(String studentName)
{
this.studentName = studentName;
}
/**
* @return Returns the colleageID.
*/
public Long getColleageID()
{
return this.colleageID;
}
/**
* @param colleageID The colleageID to set.
*/
public void setColleageID(Long colleageID)
{
this.colleageID = colleageID;
}
}
---- colleage POJO ---
public class ColleagePOJO
{
private String colleageName;
private Long colleageID;
private HashSet students;
/**
* @return Returns the colleageID.
*/
public Long getColleageID()
{
return this.colleageID;
}
/**
* @param colleageID The colleageID to set.
*/
public void setColleageID(Long colleageID)
{
this.colleageID = colleageID;
}
/**
* @return Returns the colleageName.
*/
public String getColleageName()
{
return this.colleageName;
}
/**
* @param colleageName The colleageName to set.
*/
public void setColleageName(String colleageName)
{
this.colleageName = colleageName;
}
/**
* @return Returns the students.
*/
public void setStudents(Collection students)
{
if (students != null)
{
if (this.students == null)
{
this.students = new HashSet();
}
Iterator it = students.iterator();
while (it.hasNext())
{
this.students.add(it.next());
}
}
}
/**
* @param students The students to set.
*/
public Collection getStudents()
{
return this.students;
}
}
Full stack trace of any exception that occurs:
no exception
im getting the null if i try to get through relationship
output:
going to calling student-----
b4 commit
null
Hibernate: insert into colleage (colleage_NAME, colleage_ID) values (?, ?)
Hibernate: insert into student (colleage_ID, student_NAME, student_ID) values (?, ?, ?)
after commit
null afet commit
Name and version of the database you are using:Oracle 9i
The generated SQL (show_sql=true):
Hibernate: insert into colleage (colleage_NAME, colleage_ID) values (?, ?)
Hibernate: insert into student (colleage_ID, student_NAME, student_ID) values (?, ?, ?)
Debug level Hibernate log excerpt: