This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate self referential interigity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate self referential interigity" Watch "Hibernate self referential interigity" New topic
Author

Hibernate self referential interigity

AbBaby Sharma
Greenhorn

Joined: Feb 08, 2008
Posts: 2
I was doing experiment with Hibernate self referential interigity and created following structure in hbm file and database side.

Database script:
################################################################
CREATE TABLE CATEGORY
(
CATID INTEGER NOT NULL,
CATNAME VARCHAR2(255 BYTE) NOT NULL,
PARENTID INTEGER DEFAULT 0
)


ALTER TABLE CATEGORY ADD (
CONSTRAINT FK_CATEGORY FOREIGN KEY (PARENTID)
REFERENCES CATEGORY (CATID));
########################################################

Hbm File
#######################################################
<?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="com.abhi.test.hibernate.dto.CategoryDTO" table="CATEGORY">
<id name="catId" type="int" column="CATID">
<generator class="assigned" />
</id>
<property name="catName" column="CATNAME" type="java.lang.String" />
<one-to-one name="parentCategory" />



<set name="childCategories"
table="CATEGORY"
inverse="true"
lazy="true"
>
<key column="PARENTID"/>
<many-to-many class="com.abhi.test.hibernate.dto.CategoryDTO" column="CATID" />


</set>
</class>

</hibernate-mapping>
######################################################################

Java Code:
###################################################################
public static void main(String[] args) {
Session session = HibernateUtil.currentSession();
Transaction transaction = session.beginTransaction();
try{

Query query =session.createQuery("From com.abhi.test.hibernate.dto.CategoryDTO");
System.out.println(query.getQueryString());
List list = query.list();
System.out.println(list.size());
for(Iterator i=list.iterator();i.hasNext() {
CategoryDTO categoryDTO = (CategoryDTO)i.next();

System.out.println("Child Categories----"+categoryDTO.getChildCategories());
System.out.println(categoryDTO.getParentCategory().getParentCategory().getParentCategory().getCatId());

}
transaction.commit();

}catch (Exception e) {
// transaction.rollback();
e.printStackTrace();
}
session.close();
}
##################################################


In above java code it is creating infinite CategoryDTO object struture but it is working fine without giving any memory problem.

Anyone please suggest what is happening over here or am i doing something wrong.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

It might look like it is infinite, but both JVM Heap and object references are smart enough to allow circular references and not over create unnecessary objects. And Hibernate will only ever point to one instance of an Entity with the unique id. It will share this object with all its references, it will never create two instance of the same entity with the same id.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
AbBaby Sharma
Greenhorn

Joined: Feb 08, 2008
Posts: 2
Thanks Mark.

I got it and tested it is giving same object reference.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Originally posted by AbBaby Sharma:
Thanks Mark.

I got it and tested it is giving same object reference.


Cool.
 
GeeCON Prague 2014
 
subject: Hibernate self referential interigity