休眠和货币精度 [英] Hibernate and currency precision

查看:121
本文介绍了休眠和货币精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个hibernate映射如下:




 <休眠映射> 
< class name =kochman.elie.data.types.InvoiceTVOtable =INVOICE>
< id name =idcolumn =ID>
< generator class =increment/>
< / id>
< property name =datecolumn =INVOICE_DATE/>
< property name =customerIdcolumn =CUSTOMER_ID/>
< property name =schoolIdcolumn =SCHOOL_ID/>
< property name =bookFeecolumn =BOOK_FEE/>
< property name =adminFeecolumn =ADMIN_FEE/>
< property name =totalFeecolumn =TOTAL_FEE/>
< / class>
< / hibernate-mapping>

其中InvoiceTVO的变量定义为:

  private int id; 
私人日期日期;
private int customerId;
private int schoolId;
私人浮动bookFee;
私有浮动adminFee;
私有浮动totalFee;

当我在此表上执行插入操作时,出现以下错误:

  Hibernate:插入INVOICE(INVOICE_DATE,CUSTOMER_ID,SCHOOL_ID,BOOK_FEE,ADMIN_FEE,TOTAL_FEE,ID)值(?,?,?,?,?错误:0,SQLState:22001 
50156 [AWT-EventQueue-0] ERROR org.hibernate。
50156 [AWT-EventQueue-0] WARN org.hibernate.util.JDBCExceptionReporter - util.JDBCExceptionReporter - 数据截断:第1行的列'ADMIN_FEE'的数据被截断
50156 [AWT-EventQueue-0] ERROR org.hibernate.event.def.AbstractFlushingEventListener - 无法使数据库状态与会话$ b $同步b

我尝试将adminFee的类型改为double,并且这也不起作用。问题是,Hibernate确实执行了正确的插入操作,将来的select语句会返回当前的一组值,但是这个错误阻止了我对此发票进行进一步处理。



字段bookFee,adminFee和totalFee都应该是货币。它们在MySQL数据库中被定义为十进制(5,2)。



有关如何解决这个问题的任何帮助将不胜感激。

解决方案

我会使用整数类型(int,long)。从来没有任何有趣的问题,只要你可以避免溢出。


I have a hibernate mapping as follows:

<hibernate-mapping>
    <class name="kochman.elie.data.types.InvoiceTVO" table="INVOICE">
        <id name="id" column="ID">
            <generator class="increment"/>
        </id>
        <property name="date" column="INVOICE_DATE"/>
        <property name="customerId" column="CUSTOMER_ID"/>
        <property name="schoolId" column="SCHOOL_ID"/>
        <property name="bookFee" column="BOOK_FEE"/>
        <property name="adminFee" column="ADMIN_FEE"/>
        <property name="totalFee" column="TOTAL_FEE"/>
    </class>
</hibernate-mapping>    

where InvoiceTVO has variables defined as:

private int id;
private Date date;
private int customerId;
private int schoolId;
private float bookFee;
private float adminFee;
private float totalFee;

When I do an insert on this table, I get the following error:

Hibernate: insert into INVOICE (INVOICE_DATE, CUSTOMER_ID, SCHOOL_ID, BOOK_FEE, ADMIN_FEE, TOTAL_FEE, ID) values (?, ?, ?, ?, ?, ?, ?)
50156 [AWT-EventQueue-0] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 22001
50156 [AWT-EventQueue-0] ERROR org.hibernate.util.JDBCExceptionReporter - Data truncation: Data truncated for column 'ADMIN_FEE' at row 1
50156 [AWT-EventQueue-0] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session

I tried changing the type of adminFee to double, and that didn't work either. The problem is, Hibernate did actually perform the insert properly, and future select statements work to return the current set of values, but this error prevents me from doing further processing on this invoice.

The fields bookFee, adminFee, and totalFee are all supposed to be currencies. They are defined in the MySQL database as decimal(5,2).

Any help in how to resolve this would be greatly appreciated.

解决方案

I would use an integer type (int, long). Never any funny issues with those as long you can avoid overflows.

这篇关于休眠和货币精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆