Java – GenericJDBCException: could not load an entity

hibernatejavamysql

I am trying to read a lazy initialized entity ProductUnits which is a persistent set, and opened a new session for the same.

Session session = HibernateUtil.getSessionFactory().openSession();
session.update(stock.getProduct());
List<ProductUnits> units = new ArrayList<ProductUnits>(stock.getProduct().getUnits());

The problem is during the run time it throws the exception

org.hibernate.exception.GenericJDBCException: could not load an entity: [hibernate.tables.plain.Product#1]

but while doing a step by step debug it runs perfectly fine. Is it that the session.update takes some time or am I missing something

the exception stack is

    Exception in thread "AWT-EventQueue-0" org.hibernate.exception.GenericJDBCException: could not load an entity: [hibernate.tables.plain.Product#2]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1895)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:121)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:863)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:95)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at hibernate.tables.plain.Product_$$_javassist_15.getUnits(Product_$$_javassist_15.java)
at bill.mainBill.InputProductsPanel.computeStockAvailablity(InputProductsPanel.java:278)
at bill.mainBill.InputProductsPanel.findStock(InputProductsPanel.java:242)
at bill.mainBill.ProductDetailPanel.setStats(ProductDetailPanel.java:315)
at bill.mainBill.ProductDetailPanel$3.focusLost(ProductDetailPanel.java:187)
at java.awt.AWTEventMulticaster.focusLost(Unknown Source)
at java.awt.Component.processFocusEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


    Caused by: java.sql.SQLException: Invalid value for getInt() - 'N'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2727)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2815)
at org.hibernate.type.IntegerType.get(IntegerType.java:51)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:184)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:173)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2124)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
at org.hibernate.loader.Loader.getRow(Loader.java:1230)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
at org.hibernate.loader.Loader.doQuery(Loader.java:724)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
... 48 more

then mapping file is as follows

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 3, 2012 1:21:15 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
 <class name="hibernate.tables.plain.ProductUnits" table="PRODUCT_UNITS">
  <composite-id mapped="false" unsaved-value="undefined">
   <key-many-to-one class="hibernate.tables.plain.Product"
    column="Product_ID" name="product"/>
   <key-many-to-one class="hibernate.tables.plain.Unit" column="Unit_ID" name="unit"/>
  </composite-id>
  <property generated="never" lazy="false" name="basePrice" type="java.lang.Double">
   <column name="BASE_PRICE"/>
  </property>
 </class>
</hibernate-mapping>

and the product mapping

 <class name="hibernate.tables.plain.Product" table="PRODUCT">
        <id name="id" type="java.lang.Long">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="recommendedHolding" type="java.lang.Double">
            <column name="RECOMMENDED_HOLDING" />
        </property>
        <set name="units" table="Product_Units" inverse="false" lazy="true">
            <key>
                <column name="Product_ID" />
            </key>
            <one-to-many  class="hibernate.tables.plain.ProductUnits" />
        </set>
        <property name="active" type="java.lang.Character">
            <column name="ACTIVE" />
        </property>
    </class>

Best Solution

Seems that Hibernate is having a problem mapping a character value N to a java.lang.Boolean.

If you need to map java.lang.Boolean maybe it's better to use either java.lang.Boolean or org.hibernate.type.YesNoType instead of 'java.lang.Character'. You can read more in the reference.

Or you need to use java.lang.Character, follow the reference to register a custom type.

Related Question