-->
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.  [ 3 posts ] 
Author Message
 Post subject: Newbie with Hibernate - Problem with loading
PostPosted: Tue Sep 19, 2006 1:57 pm 
Beginner
Beginner

Joined: Tue Sep 19, 2006 1:48 pm
Posts: 20
Hi! I am a new Hibernate user and I am getting a problem while loading an object from my DB. I always get a "proxy" error. Insertion works properly. Where could the problem come from?

Hibernate version: 3.1.3

Mapping documents:
Code:
<hibernate-mapping package="test">
   <class
      name="User"
      table="user"
   >
      <meta attribute="sync-DAO">false</meta>
      <id
         name="Id"
         type="integer"
         column="id"
      >
         <generator class="identity"/>
      </id>

      <property
         name="Nom"
         column="nom"
         type="string"
         not-null="true"
         length="255"
      />
      <property
         name="Prenom"
         column="prenom"
         type="string"
         not-null="true"
         length="255"
      />
      <property
         name="Age"
         column="age"
         type="integer"
         not-null="true"
         length="11"
      />


   </class>   
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Code:
      Session session = HibernateUtil.currentSession();
      Transaction tx = session.beginTransaction();
      User contact = (User) session.load(User.class, new Integer(1));
      contact.setPrenom("Matthias");
      session.save(contact);
      tx.commit();
      HibernateUtil.closeSession();


Full stack trace of any exception that occurs:
Quote:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3121)
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:232)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:173)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:781)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:774)
at Test.main(Test.java:11)


Name and version of the database you are using:
MySQL


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 7:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The default is that the entity is lazy loaded. You have asked to load the entity and thus it will be a proxy (and not actually loaded). When you try to update the field it will hit the database and load the item. I suggest you use (for the moment) session.get() which will load the item or return a null. You can then check the result - just in case User (id = 1) does not exist in the db.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 20, 2006 3:07 am 
Beginner
Beginner

Joined: Tue Sep 19, 2006 1:48 pm
Posts: 20
All right, the session.get() works and my "1" ID exists!
But then, when I try to modify this object and save it with session.save, nothing happens in my table.
Why? Is get() read only? Why load() doesn't work?


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