These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Unable to do a get Operation through relationship
PostPosted: Fri Aug 04, 2006 3:45 am 
Newbie

Joined: Thu Aug 03, 2006 1:31 am
Posts: 5
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:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 5:29 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
I don't see code that adds Students to the collection.

Code:
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);
}


Should you be adding the student created into ColleagePOJO. You should have a helper method called "addStudent" that adds it to the Set. It may look something like this:

Student stud = createStudent(studentID, studentName, colleageID);
colleage.addStudent(stud);


Since you have given the "cascade" option as "all", Once you have persisted "Colleage" objects, you can add Students to the ColleagePOJO and simple called "save" and hibernate will insert rows to both the Parent (ColleagePOJO) and Student tables.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 5:39 am 
Newbie

Joined: Thu Aug 03, 2006 1:31 am
Posts: 5
Hi,
In EJB im able to get the collection even if i dont set(add) the student collection to colleage.

Is there any setting in Hibernate to support this


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 5:45 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
I think this is not related to Hibernate. If you are trying this program standalone, I would reccomend a good IDE like Eclipse/Netbean etc. and debug the flow step by step.

For collections you will need to add objects to the collections for Hibernate to know what are the relating objects that it needs to persist. I'm not sure of your EJB code.

-Anand


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 5:53 am 
Newbie

Joined: Thu Aug 03, 2006 1:31 am
Posts: 5
HI,
I tried it in a standalone in eclipse my problem is im able to getstudents only if i setstudents in colleage. since i have said the relationship in colleage.hbm.xml is there any way to getstudents without setting it.

In the case of EJB if we specify the relationship (CMR) it will take care of it.
Is there any setting in Hibernate to enable this functionality in relationship


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 7:32 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Hibernate will take care of loading Students if they are already persistent but for new Students, I beleive you will have to set (or add) them to your underling collection (i.e. Set). Hibernate manages relationships once they are persistent. Relationships of transient objects (pre persistence) is probably the job of the application.

I'd be interested to find out how CMR would help for creating new Students.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 7:43 am 
Newbie

Joined: Thu Aug 03, 2006 1:31 am
Posts: 5
Hi,
my project has been implemented in EJB im trying to convert it to Hinbernet. In EJB the Container manages the Relationship even if u dont set in the relationship a get will return the info but this doesn't happen in Hibernate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 8:17 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Hibernate does provide the ability to load relationships automatically when the containing entity is fetched. However while saving if you need to save newer child objects there's no way hibernate would automatically add this to your collection.

I don't beleive CMR does this either. What you are referring to is loading of related entities from the DB tables. Hibernate is capable of the same.

When you save a new persistent object of type ColleagePOJO, you will need to ensure the underlying collections have the corresponding child objects. Otherwise Hibernate will not know of their relationship to this particular object.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.