-->
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.  [ 9 posts ] 
Author Message
 Post subject: Hibernate initialize
PostPosted: Wed Apr 16, 2008 8:12 pm 
Newbie

Joined: Wed Apr 16, 2008 7:49 pm
Posts: 10
I'm having a simple question regarding collection initialize for the following code:

Session session = HibernateUtil.getSession();
Query q = session.createQuery("FROM Parent WHERE status = :p");

List result q = q.list();

Assuming that Parent class has 1 collection property :
- childrens List that would be lazy loaded.

I need to eagerly fetch the "children" collections only for some "parents" that satisfy a criteria. So I did:

for( Parent p : result )
{
if ( p.name = "John" ) {
Hibernate.initialize(p.getChildrens());
}
}

To my surprise after the loop all the parents have the childrens initialized and I was hoping to see that only for John I initialized the childrens.


Am I doing anything wrong? Thanks,
Sivec


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 17, 2008 12:24 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Try this:

Code:
if ( p.name.equals("John") ) {
p.getChildrens();
}


instead of:

Quote:
if ( p.name = "John" ) {
Hibernate.initialize(p.getChildrens());
}



Your if condition is wrong in the first place. But I guess its a typing mistake.... otherwise it will throw a compilation error. Also try removing the Hiberate.initialize. As long as you are in the current session scope, a simple call to the getChildren method should populate the children for the particular parent object. Ofcourse, Hibernate.initialize() performs the same operation and should work. Just check your if condition again. Thats the only problem I can see.

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 17, 2008 8:58 am 
Newbie

Joined: Wed Apr 16, 2008 7:49 pm
Posts: 10
Forgot to tell that the code was just pseudo code not the real one. It seems that Hibernate.initialize(parent.getChildrens() ) won't initialize only the childrens for a particular parent but all the childrens for all the parents from the initial response set.

So, having a result set

Query q = session.createQuery("Select Parent p");

List result = q.list();

Now I want to initialize only the childrens for parents with name John; and leave the others alone; so I was doing

for(Parent p : result )
{
if ("John".equals(p.getName() ) {
Hibernate.initialize(p.getChildrens() );
}
}


After the loop I see all the childrens initialized even for parents with name different than John.


I suspect that this is a BUG in the way Hibernate.initialize works.


Sivec


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 17, 2008 10:22 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
have you define lazy=true in your hbm.xml? I tried it with Hibernate 3.2.5, it runs well, only "John" children's loaded.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 17, 2008 4:25 pm 
Newbie

Joined: Wed Apr 16, 2008 7:49 pm
Posts: 10
Here is the code:


PARENT - mapping

<?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 package="com.app">
<class name="Parent"
table="Parent"
dynamic-update="true"
dynamic-insert="true"
lazy="true">

<id name="id" type="long" column="id">
<generator class="sequence">
<param name="sequence">SQ_ID</param>
</generator>
</id>


<list name="children" table="CHILDREN" cascade="all" batch-size="100" lazy="true">
<key column="PARENT_ID" />
<index column="INDEX_COL"/>
<one-to-many class="com.ssgm.Children" />
</list>

</class>
</hibernate-mapping>


CHILDREN mapping ======================

<?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 package="com.app">
<class name="Children"
table="Children"
dynamic-update="true"
dynamic-insert="true"
lazy="true">

<id name="id" type="long" column="id">
<generator class="sequence">
<param name="sequence">SQ_ID</param>
</generator>
</id>

<property name="indexCol" column="index_col" update="false" insert="false"/>
</class>
</hibernate-mapping>


JAVA CODE ==================================

public class Parent {

private Long id;
private List children = new ArrayList();

public Parent(){

}

public Long getId(){
return this.id;
}

public void setId(Long id){
this.id = id;
}



public List getChildren() {
return children;
}

public void setChildren(List children) {
this.children = children;
}

}

=========================================

public class Children {

private Long id;
private Long indexCol;

public Long getIndexCol() {
return indexCol;
}

public void setIndexCol(Long indexCol) {
this.indexCol = indexCol;
}

public Children(){

}

public Long getId(){
return this.id;
}

public void setId(Long id){
this.id = id;
}

}
==============================================



Session session= HibernateUtil.getSessionFactory().openSession();

Transaction tx = session.beginTransaction();


Query qry = session.createQuery("FROM Parent t WHERE t.id in (1,2) ");


List<Parent> result2 =qry.list();


for(Parent ts: result2){
Hibernate.initialize(ts.getChildren());
break;
}

tx.commit();

session.close();

for(Parent ts: result2){
System.out.println( ts.getChildren().size() );
}


I would expect that the second parent not to have the childrens initialized and the lazy exception to be thrown.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 18, 2008 12:57 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Quote:
for(Parent ts: result2){
Hibernate.initialize(ts.getChildren());
break;
}


Try this instead

for(Parent ts: result2){
ts.getChildren();
break;
}

Just to check if the problem is only with Hibernate.initialize()

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 18, 2008 9:04 pm 
Newbie

Joined: Wed Apr 16, 2008 7:49 pm
Posts: 10
ts.getChildren() won't init the collection, but isEmpty and size() shoud do it. Unfortunatelly, for some reasons all parents childrens gets initialized; same happens with Hibernate.initialize.

I suspect I have something wrong in the mapping files. Any help?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 19, 2008 5:03 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
testos123 wrote:
ts.getChildren() won't init the collection, but isEmpty and size() shoud do it. Unfortunatelly, for some reasons all parents childrens gets initialized; same happens with Hibernate.initialize.

I suspect I have something wrong in the mapping files. Any help?


try to remove batch-size="100" in tag list


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 8:51 am 
Newbie

Joined: Wed Apr 16, 2008 7:49 pm
Posts: 10
Thanks!

Removing batch attribute fixed the problem; Not sure why Hibernate.initialize would be messed up by "batch" attribute.

Sivec


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.