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

Hibernate self referential interigity

 
AbBaby Sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
AbBaby Sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark.

I got it and tested it is giving same object reference.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by AbBaby Sharma:
Thanks Mark.

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


Cool.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic