hibernate 在 session.save(object) 上抛出 ClassCastException

hiberante throws a ClassCastException on session.save(object)

在我的项目中,我试图用 hiberante 的 session.save 保存一个名为 Product 的对象,直到现在它一直有效,但突然我遇到了这个异常:

Caused by: java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String
at org.hibernate.type.descriptor.java.StringTypeDescriptor.unwrap(StringTypeDescriptor.java:39)
at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor.doBind(VarcharTypeDescriptor.java:64)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:90)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2818)
at org.hibernate.persister.entity.AbstractEntityPersister.bindValues(AbstractEntityPersister.java:3025)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
at erion.db.connection.HibernateQueries.save(HibernateQueries.java:43)
at erion.db.model.ProductManager.saveProduct(ProductManager.java:25)
at erion.view.ProductView$NewProductLayout.buttonClick(ProductView.java:86)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
... 38 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 07.11.2014 11:50:20 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="erion.db.beens.Product" table="product" catalog="mydb">
        <id name="idproduct" type="java.lang.Integer">
            <column name="idproduct" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="name" length="45" />
        </property>
        <property name="price" type="string">
            <column name="price" length="45" />
        </property>
        <property name="rateofmwst" type="java.lang.Double">
            <column name="rateofmwst" precision="22" scale="0" />
        </property>
        <set name="deliverynoteHasProducts" table="deliverynote_has_product" inverse="true" lazy="false" fetch="select">
            <key>
                <column name="product_idproduct" not-null="true" />
            </key>
            <one-to-many class="erion.db.beens.DeliverynoteHasProduct" />
        </set>
    </class>
</hibernate-mapping>

我通过 hiberante 逆向工程生成了对象并得到了这个类:

public class Product implements java.io.Serializable {

    private Integer idproduct;
    private String name;
    private Double price;
    private Double rateofmwst;
    private Set deliverynoteHasProducts = new HashSet(0);

  ...

}

这可能是因为您在映射文件中将属性 "price" 的类型定义为 String,而在模型 class.

中它是 Double

价格列在列定义中声明为字符串。因此,当您从请求中传递一个 Double 值并尝试保留它时,它会给您类型转换异常。尝试更改产品 class 或数据库中的值。

正如我在评论中提到的: 您的 POJO 模型声明了一个 Double pice 属性。 您的 HBM 映射将其视为字符串。

这会导致转换过程中出现问题。

小贴士: - 使用 Long 而不是整数作为 PrimaryKey - 使用 BigDecimal 而不是 Double 作为价格 属性 - 在精度方面,double 不是 goag。

如果这是您的演示应用程序,那没关系。但在现实生活中,我会用我的小费:)