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

时间:2015-01-05 11:29:59

标签: java hibernate

在我的项目中我试图用hiberante的session.save保存一个名为Product的Object,直到现在它总是有效,但突然间我得到了这个例外:

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$1.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$4.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$1.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逆向工程生成了Object并得到了这个类:

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

  ...

}

3 个答案:

答案 0 :(得分:2)

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

这会在施法期间导致问题。

提示: - 使用Long而不是整数作为PrimaryKey - 使用BigDecimal而不是Double作为price属性 - 在精度方面,double不是goag。

如果这是您的演示应用,那么没关系。但在现实生活中,我会提出我的建议:)

答案 1 :(得分:0)

这可能是因为您在映射文件中将属性“price”的类型定义为String,而在模型类中它是Double。

答案 2 :(得分:0)

价格列在列定义中声明为String。因此,当您从请求中传递Double值并尝试保留它时,它会为您提供类型转换异常。尝试更改Product类或Database中的值。