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 :)