GenericJDBCException:无法加载实体

时间:2012-04-06 07:22:15

标签: java mysql hibernate

我正在尝试读取一个持久集的惰性初始化实体ProductUnits,并为此打开一个新会话。

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

问题是在运行时它抛出异常

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

但在进行一步一步的调试时,它运行得很好。是session.update需要一些时间还是我错过了什么

异常堆栈是

    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

然后映射文件如下

<?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>

和产品映射

 <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>

6 个答案:

答案 0 :(得分:3)

似乎Hibernate在将字符值N映射到java.lang.Boolean时遇到问题。

如果您需要映射java.lang.Boolean,最好使用java.lang.Booleanorg.hibernate.type.YesNoType代替'java.lang.Character'。您可以在reference中阅读更多内容。

或者您需要使用java.lang.Character,请按照reference注册自定义类型。

答案 1 :(得分:2)

有你的问题:

 Caused by: java.sql.SQLException: Invalid value for getInt() - 'N'

答案 2 :(得分:2)

将属性类型从Character更改为String

<property name="active" type="java.lang.String">
    <column name="ACTIVE" />
</property>

答案 3 :(得分:2)

我怀疑您的问题是由您的映射设置引起的:

 <composite-id mapped="false" unsaved-value="undefined">

注意:

  

unsaved-value(可选 - 默认为“合理”值):a   标识符属性值,表示实例是新的   实例化(未保存),将其与分离的实例区分开来   在之前的会话中保存或加载。

该设置导致PRODUCT_UNITS无法保留。

Hibernate3几乎不需要unsaved-value属性。

答案 4 :(得分:1)

我发现另一个引用声明hibernate将一个字符转换为int(即使文档状态字符是可用的数据类型......)。其他答案应该解决这个问题。或者我建议你只使用varchar(1) - 相同的最终结果。

IE)

@Column(name = "column", nullable = false, length = 1)
public String getColumn() {
    return column;
}

    <property name="active" type="java.lang.String">
        <column name="ACTIVE" length="1"/>
    </property>

答案 5 :(得分:0)

请注意,检查persistence.xml文件添加适当的类 注意:-datistence.xml文件未正确匹配