-->
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: Mapping many-to-one problem
PostPosted: Sat Aug 06, 2011 7:07 am 
Newbie

Joined: Tue Jan 12, 2010 12:06 pm
Posts: 3
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.xml

Code:
<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.xml


Code:
<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.


Top
 Profile  
 
 Post subject: Re: Mapping many-to-one problem
PostPosted: Mon Aug 08, 2011 4:02 am 
Newbie

Joined: Tue Jan 12, 2010 12:06 pm
Posts: 3
Hi again,

okay, we changed the hbm file of Laboratory from

Quote:
<many-to-one name="laboratoryType" class="LaboratoryType" column="fkLaboratoryType" property-ref="idLaboratoryType" />


to

Code:
<many-to-one name="laboratoryType" class="LaboratoryType" column="idLaboratoryType" cascade="all" not-null="true" fetch="select"/>


And we changed also the name of the column in the database. Now both columns, in Laboratory and LaboratoryType, have the same name: idLaboratoryType.

Now it doesn't throw any Exception, but when we call the session.get() of hibernate, we do not get all the object LaboratoryType, even if using HQL evironment we do.

Does anybody have any idea about how to solve it?

Thank you again.

Daniel.


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.