-->
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.  [ 10 posts ] 
Author Message
 Post subject: cglib error on first use of hibernate
PostPosted: Thu Nov 03, 2005 12:26 pm 
Newbie

Joined: Thu Nov 03, 2005 12:08 pm
Posts: 6
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="de.soft_nrg.www.xDMS.Operation" table="tkp.joboperationsview">
      <composite-id>
          <key-property name="keyOperation" column="noperationkey"/>
          <key-property name="keyJob" column="njobkey"/>
      </composite-id>
                <!-- commented out fields -->
   </class>
</hibernate-mapping>[/b]


[b]Code between sessionFactory.openSession() and session.close():
Code:
Transaction tx = session.beginTransaction();
Operation op = (Operation) session.load(Operation.class, jobKey);

//
//commit is not reached, anyway!!!, previous load call throws!!!
//    :-((
tx.commit();



Full stack trace of any exception that occurs:
error on
Code:
Exception in thread "main" org.hibernate.PropertyAccessException: exception getting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) getter of de.soft_nrg.www.xDMS.Operation.?
   at org.hibernate.tuple.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:79)
   at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:307)
   at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:158)
   at org.hibernate.engine.EntityKey.getHashCode(EntityKey.java:68)
   at org.hibernate.engine.EntityKey.<init>(EntityKey.java:41)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:76)
   at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:809)
   at org.hibernate.impl.SessionImpl.load(SessionImpl.java:731)
   at org.hibernate.impl.SessionImpl.load(SessionImpl.java:724)
   at de.soft_nrg.www.xDMS.HibernateTest.testOperationLoadThroughHibernate(HibernateTest.java:59)
   at de.soft_nrg.www.xDMS.HibernateTest.main(HibernateTest.java:69)
Caused by: java.lang.ClassCastException: java.lang.String
   at de.soft_nrg.www.xDMS.Operation$$BulkBeanByCGLIB$$966d66f8.getPropertyValues(<generated>)
   at net.sf.cglib.beans.BulkBean.getPropertyValues(BulkBean.java:48)
   at org.hibernate.tuple.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:76)
   ... 10 more



Name and version of the database you are using:postgresql 8

The generated SQL (show_sql=true): none
Code:

[b]Debug level Hibernate log excerpt:
### Hibernate Core ###
log4j.logger.net.sf.hibernate=debug

### log schema export/update ###
log4j.logger.net.sf.hibernate.tool.hbm2ddl=info

### log cache activity ###
log4j.logger.net.sf.hibernate.cache=warn

### log JDBC bind parameters ###
log4j.logger.net.sf.hibernate.type=warn

[/b]

Thanks, I hope someone is listening

_________________
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 12:41 pm 
Beginner
Beginner

Joined: Tue Aug 23, 2005 3:52 pm
Posts: 26
If you have a composit key and you want to retrieve data by key using session.load() you have to defind composit key as an object.

see http://www.hibernate.org/hib_docs/v3/re ... ompositeid


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 12:55 pm 
Newbie

Joined: Thu Nov 03, 2005 12:08 pm
Posts: 6
Thank you! Many thanks!

Still, one more question:
With the same configuration except the id, let's say i try again with
Code:
<class name="de.soft_nrg.www.xDMS.Operation" table="tkp.joboperationsview">
        <id name="keyJob" column="njobkey">
            <generator class="native"/>
        </id>
<class/>
....


another exception is thrown, still meaningless to me!!

Code:
19:00:19,740 ERROR LazyInitializationException: could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:56)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
   at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:133)
   at de.soft_nrg.www.xDMS.Operation$$EnhancerByCGLIB$$aa6f6b28.toString(<generated>)
   at java.lang.String.valueOf(String.java:2577)
   at java.io.PrintStream.print(PrintStream.java:616)
   at java.io.PrintStream.println(PrintStream.java:753)
   at de.soft_nrg.www.xDMS.HibernateTest.main(HibernateTest.java:70)
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:56)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
   at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:133)
   at de.soft_nrg.www.xDMS.Operation$$EnhancerByCGLIB$$aa6f6b28.toString(<generated>)
   at java.lang.String.valueOf(String.java:2577)
   at java.io.PrintStream.print(PrintStream.java:616)
   at java.io.PrintStream.println(PrintStream.java:753)
   at de.soft_nrg.www.xDMS.HibernateTest.main(HibernateTest.java:70)


I owe you, big time
Thanks, a lot

_________________
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 1:03 pm 
Beginner
Beginner

Joined: Tue Aug 23, 2005 3:52 pm
Posts: 26
LazyinItialization exception is thrown when you try to acces a component that is lazily loaded after session i closed. I can't tell anything more then that because I can't see your the rest of you config file or you code.

FYI.
Don't forget to rate positings.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 1:09 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
this link is a good start. the stuff about interceptors may not be relevant to your case right now, but I think you'll find some useful information here.
http://www.hibernate.org/43.html


quoted from that page.

Why can't Hibernate just load objects on demand?

Every month someone has the idea that Hibernate could instead of throwing a LazyInitializationException just open up a new connection to the database (effectively starting a new Session) and load the collection or initialize the proxy that has been touched on-demand. Of course, this idea, while brilliant at first, has several shortcomings that only appear if you start to think about the consequences of ad-hoc transactional access.

If Hibernate would, hidden from the developer and outside of any transaction demarcation, start random database connections and transactions, why have transaction demarcation at all? What happens when Hibernate opens a new database connection to load a collection, but the owning entity has been deleted meanwhile? (Note that this problem does not appear with the two-transaction strategy as described above - the single Session provides repeatable reads for entities.) Why even have a service layer when every object can be retrieved by simply navigating to it? How much memory should be consumed by this and which objects should be evicted first? All of this leads to no solution, because Hibernate is a service for online transaction processing (and certain kinds of batch operations) and not a "streaming objects from some persistent data store in undefined units of work"-service. Also, in addition to the n+1 selects problem, do we really need an n+1 transaction and connection problem?

The solution for this issue is of course proper unit of work demarcation and design, supported by possibly an interception technique as shown in the pattern here, and/or the correct fetch technique so that all required information for a particular unit of work can be retrieved with minimum impact, best performance, and scalability.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 1:12 pm 
Newbie

Joined: Thu Nov 03, 2005 12:08 pm
Posts: 6
thanks again, the credits are on the way :-)
these are my files, maybe you can figure it out

Operation.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="de.soft_nrg.www.xDMS.Operation" table="tkp.joboperationsview">
        <id name="keyJob" column="njobkey">
            <generator class="native"/>
        </id>
   </class>
</hibernate-mapping>



hibernate.cfg.xml:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
      <!--   <property name="connection.datasource">java:comp/env/jdbc/quickstart</property> -->

      <property name="connection.driver_class">sun.jdbc.odbc.JdbcOdbcDriver</property>
      <property name="connection.url">jdbc:odbc:tkp_pgs</property>
      <property name="connection.user">tkp</property>
      <property name="connection.password">tkp</property>
       
        <property name="hibernate.cglib.use_reflection_optimizer">false</property>
      <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
      <property name="show_sql">false</property>
      
      <!-- Mapping files -->
      <mapping resource="Operation.hbm.xml"/>
   </session-factory>
</hibernate-configuration>


java test class:
Code:
package de.soft_nrg.www.xDMS;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import de.soft_nrg.www.xDMS.Operation;

/**
* Hibernate helper class
*
* @author chris
*/
class HibernateUtil {
   public static final SessionFactory sessionFactory;
   static {
      try {
         sessionFactory = new Configuration()
            .configure()
            //.addResource("Operation.hbm.xml")
            //.addClass(de.soft_nrg.www.xDMS.Operation.class)
            .buildSessionFactory();
      } catch (Throwable ex) {
         System.err.println("Initial SessionFactory creation failed. " + ex);
         throw new ExceptionInInitializerError(ex);
      }
   }
   public static final ThreadLocal session = new ThreadLocal();
   public static Session currentSession() throws HibernateException {
      Session s = (Session) session.get();
      if (s == null) {
         s = sessionFactory.openSession();
         session.set(s);
      }
      return s;
   }
   public static void closeSession() throws HibernateException {
      Session s = (Session) session.get();
      if (s != null)
         s.close();
      session.set(null);
   }
}


/**
* Hibernate test class
*
* @author chris
*/
public class HibernateTest {
   
   public Operation testOperationLoadThroughHibernate(String jobKey, String opKey) {
      Session session = HibernateUtil.currentSession();
      Transaction tx = session.beginTransaction();

      Operation op = (Operation) session.load(Operation.class, jobKey);
//      List result = session.createQuery("from Event").list();
      
      tx.commit();
      session.close();

      return op;
   }
   public static void main(String[] args) {
      HibernateTest test = new HibernateTest();
      Operation op = test.testOperationLoadThroughHibernate("AA00000002", "1");
      System.out.println(op);
   }
}


thanks

_________________
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 1:20 pm 
Newbie

Joined: Thu Nov 03, 2005 12:08 pm
Posts: 6
kochcp wrote:

Why can't Hibernate just load objects on demand?

Every month someone has the idea that Hibernate could instead of throwing a LazyInitializationException just open up a new connection to the database (effectively starting a new Session) and load the collection or initialize the proxy that has been touched on-demand. Of course, this idea, while brilliant at first, has several shortcomings that only appear if you start to think about the consequences of ad-hoc transactional access.
....


Thanks, but unless I follow the link [this might be a very good ideea], I'm in even deeper trouble by reading your answer.
This is my first test with hibernate and it seems I haven't read enough. (the reference and the H in action, about the first 3-4 chapters)
Thanks

_________________
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 1:24 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Code:
public class HibernateTest {
   
   public Operation testOperationLoadThroughHibernate(String jobKey, String opKey) {

      Session session = HibernateUtil.currentSession();

      Operation op = (Operation) session.load(Operation.class, jobKey);

      return op;

   }

   public static void main(String[] args) {
     Session session = HibernateUtil.currentSession();
  try{
       
    Transaction tx = session.beginTransaction();
     
      HibernateTest test = new HibernateTest();
      Operation op = test.testOperationLoadThroughHibernate     ("AA00000002", "1");
      System.out.println(op);
     
         tx.commit();
 
 
}finally{
        session.close();
}


   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 1:40 pm 
Newbie

Joined: Thu Nov 03, 2005 12:08 pm
Posts: 6
Thanks baliukas!
It works now, I got it!

Many thanks

_________________
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 1:43 pm 
Newbie

Joined: Thu Nov 03, 2005 12:08 pm
Posts: 6
kochcp wrote:

Why can't Hibernate just load objects on demand?

Every month someone has the idea that Hibernate could instead of throwing a LazyInitializationException just open up a new connection to the database
....


I just thought that closing the session does no harm to the detached object.
I mean, the object is loaded inside the session, but when I close the session I still have the object's data right there.

Interesting link... thanks

_________________
Chris


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