Hi,
The development team I work with has just started to use Hibernate and we have a very important problem, which does not let us to continue. We tried everything, and I believe it is a little detail which is not well configured.
I need to manage information about certain Laboratories. These laboratories can be classified in different types. We created two tables in a MySQL database, as it is shown below:
Code:
CREATE TABLE `Laboratory` (
`idLaboratory` int(11) NOT NULL auto_increment,
`fkLaboratoryType` int(11) NOT NULL default '0',
`Name` varchar(45) NOT NULL,
PRIMARY KEY (`idLaboratory`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
CREATE TABLE `LaboratoryType` (
`idLaboratoryType` int(11) NOT NULL auto_increment,
`Name` varchar(60) default NULL,
`Description` varchar(100) default NULL,
PRIMARY KEY (`idLaboratoryType`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
We need to manage the information about the type of the laboratory, so our 'beans' are written like this:
Code:
public class Laboratory {
private Integer idLaboratory;
private LaboratoryType laboratoryType;
private String name;
public Laboratory() {
}
//Getters and Setters
}
public class LaboratoryType {
private Integer idLaboratoryType;
private String name;
private String description;
public LaboratoryType() {
}
//Getters and Setters
}
The last thing we need is to do the mapping:
Laboratory.hbm.xmlCode:
<hibernate-mapping>
<class name="Laboratory" table="Laboratory" catalog="db">
<id name="idLaboratory" type="java.lang.Integer">
<column name="idLaboratory" />
<generator class="native" />
</id>
<many-to-one name="laboratoryType" class="LaboratoryType" column="fkLaboratoryType" property-ref="idLaboratoryType" />
<property name="name" type="string">
<column name="Name" length="45" not-null="true" />
</property>
</class>
</hibernate-mapping>
LaboratoryType.hbm.xmlCode:
<hibernate-mapping>
<class name="LaboratoryType" table="LaboratoryType" catalog="db">
<id name="idLaboratoryType" type="java.lang.Integer">
<column name="idLaboratoryType" />
<generator class="native" />
<!-- <generator class="identity" /> -->
</id>
<property name="name" type="string">
<column name="Name" length="60" />
</property>
<property name="description" type="string">
<column name="Description" length="100" />
</property>
</class>
</hibernate-mapping>
The thing is, we are using eclipse for the development, and we have installed the hibernate plugin. When we try to execute a simple query like this:
Code:
select laboratory from Laboratory as laboratory
We get an error (NullPointerException):
Quote:
java.lang.NullPointerException
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1777)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:674)
at org.hibernate.type.EntityType.resolve(EntityType.java:434)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:140)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
at org.hibernate.loader.Loader.doQuery(Loader.java:773)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
at org.hibernate.loader.Loader.list(Loader.java:2187)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.console.HQLQueryPage.getList(HQLQueryPa
......
Does anybody see anything we're doing wrong and we are not aware of? We have tried everything we know. Still no result.
Can you help us? Thank you very much.
Regards,
Daniel.