-->
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.  [ 2 posts ] 
Author Message
 Post subject: Help with HQL! need to avoid lazy inizialization issue!
PostPosted: Fri Jun 19, 2009 10:25 am 
Newbie

Joined: Fri Jun 19, 2009 9:47 am
Posts: 3
Hi everibody!
I using hibernate 3 in my web app and I'm trying to access to some objects in the DB with HQL but it seems much more difficult than a normal SQL query language. Probabily because I'm new in hibernate.
Here is my database:

Experiment POJO class
Quote:
public class Experiment implements java.io.Serializable {


private Short experimentId;
private String type;
private Date lastUpdate;
private Set<Feature> features = new HashSet<Feature>(0);

constructors, getter and setter methods etc..
}


Feature POJO class
Quote:
public class Feature implements java.io.Serializable {


private FeatureId id;
private Experiment experiment;
private String value;

getter and setter methods...
}


Experiment.hbm.xml
Quote:
<hibernate-mapping>
<class name="model.Experiment" table="experiment" catalog="experimentdb">
<id name="experimentId" type="java.lang.Short">
<column name="experiment_id" />
<generator class="identity" />
</id>
<property name="type" type="string">
<column name="type" length="50" not-null="true" />
</property>
<property name="lastUpdate" type="timestamp">
<column name="last_update" length="19" not-null="true" />
</property>
<set name="features" inverse="true">
<key>
<column name="experiment_id" not-null="true" />
</key>
<one-to-many class="model.Feature" />
</set>
</class>
</hibernate-mapping>


Feature.hbm.xml
Quote:
<hibernate-mapping>
<class name="model.Feature" table="feature" catalog="experimentdb">
<composite-id name="id" class="model.FeatureId">
<key-property name="experimentId" type="short">
<column name="experiment_id" />
</key-property>
<key-property name="name" type="string">
<column name="name" length="50" />
</key-property>
</composite-id>
<many-to-one name="experiment" class="model.Experiment" update="false" insert="false" fetch="select">
<column name="experiment_id" not-null="true" />
</many-to-one>
<property name="value" type="string">
<column name="value" length="150" not-null="true" />
</property>
</class>
</hibernate-mapping>


The class Feature has as primary key name and experiment_id so a new class was needed. Here it is:
Quote:
public class FeatureId implements java.io.Serializable {


private short experimentId;
private String name;

constructor, getter and setter methods...
}


OK, now the DB is defined.
My problem is that in my web app I have to list all experiments first, at this stage without all the features. For this reason the lazy initialization it's ok for me.

find("from Experiment");

But after a user can select a partiular experiment and see in more details all experiment's features. At this point, the information that I have is the experimentId.
Due to the lazy initialization, I cannot just retrive the experiment by Id, because if I try to access to the fatures collection in the experiment class a lazy initialization exception arise. For this reason I have to create a HQL query that allow me to get the informations I need. Something like:

find("from Feature where Experiment=(from Experiment where experimentId=?)",id);

but of course this doesn't work

Quote:
Request processing failed; nested exception is java.lang.NullPointerException


debugging in the variable id there is the right information. The exception is thrown when hibernate try to execute the query...

I hope that I was able to explain the point clearly
Please help!
Many thanks in advance :)


Top
 Profile  
 
 Post subject: Re: Help with HQL! need to avoid lazy inizialization issue!
PostPosted: Fri Jun 19, 2009 10:58 am 
Newbie

Joined: Fri Jun 19, 2009 9:47 am
Posts: 3
I'm even trying this query

find("select e.features from Experiment e inner join fetch e.features as f where e.experimentId=?",id);

but
Quote:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=f,role=model.Experiment.features,tableName=experimentdb.feature,tableAlias=features1_,origin=experimentdb.experiment experiment0_,colums={experiment0_.experiment_id ,className=model.Feature}}] [select e.features from model.Experiment e inner join fetch e.features as f where e.experimentId=1]
at org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:195)
at org.hibernate.hql.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:705)
at org.hibernate.hql.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:529)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:645)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)


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