• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Hibernate self referential interigity

 
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.
 
ranger
Posts: 17344
11
Mac IntelliJ IDE 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
Posts: 17344
11
Mac IntelliJ IDE 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!