6

我正在尝试读取一个惰性初始化实体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>
4

6 回答 6

3

似乎 Hibernate 在将字符值映射Njava.lang.Boolean.

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

或者你需要使用java.lang.Character,按照参考注册一个自定义类型。

于 2012-04-06T11:33:50.540 回答
2

你的问题是:

 Caused by: java.sql.SQLException: Invalid value for getInt() - 'N'
于 2012-04-06T08:36:06.303 回答
2

那么将属性类型从字符更改为字符串

<property name="active" type="java.lang.String">
    <column name="ACTIVE" />
</property>
于 2012-04-20T14:02:02.813 回答
2

我怀疑您的问题来自您的映射设置:

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

笔记:

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

该设置导致PRODUCT_UNITS不持久。

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

于 2012-04-24T14:51:30.333 回答
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>
于 2012-04-22T03:40:04.503 回答
-1

请检查 persistence.xml 文件添加正确的类 注意:-persistence.xml 文件的数据源未正确匹配

于 2012-07-10T04:53:08.410 回答