更新hibernate对象时将数据类型nvarchar转换为十进制错误时出错

时间:2012-03-15 15:37:50

标签: java hibernate

我有一个使用hibernate的JSF应用程序 我为多边形加载了一组点,想要更新点。

我更新了点信息,但是当我尝试提交更改时,出现以下错误:

    WARNING: SQL Error: 8114, SQLState: S0005
    SEVERE: Error converting data type nvarchar to decimal.
    SEVERE: Could not synchronize database state with session
    org.hibernate.exception.SQLGrammarException: could not update: [hibernate.TbPolygonPoint#937]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)

我很困惑,因为我在试图更新的对象中没有任何字符信息。

有谁知道我为什么会收到此错误,我该如何解决? 感谢。

这是更新代码。尝试执行提交时出错:

    public int updatePolygonPoint( long polyPointId, int sGroup, int group, double lat, double lon )
    {
        int stat = 0;
        TbPolygonPoint point = null;
        try
       {
            BigDecimal bdLat = new BigDecimal(lat);
            BigDecimal bdLon = new BigDecimal(lon);
            org.hibernate.Transaction tx = session.beginTransaction();
            point = (TbPolygonPoint)session.get(TbPolygonPoint.class, polyPointId);
            point.setIsuperGroupId(sGroup);
            point.setIgroupId(group);
            point.setDcLatitude(bdLat);
            point.setDcLongitude(bdLon);
            try
            {
                session.update(point);
                tx.commit();
                this.session = HibernateUtil.getSessionFactory().openSession();
                stat = 1;
            }
            catch(Exception e)
            {
                tx.rollback();
                e.printStackTrace();
                status = e.getMessage();
                stat = -1;
            }
        }
        catch( Exception ex )
        {
             ex.printStackTrace();
            status = ex.getMessage();
            stat = -1;
        }
        return stat;
    }

以下是该对象的java代码。

    import java.math.BigDecimal;
    import java.util.HashSet;
    import java.util.Set;

    /**
    * TbPolygonPoint generated by hbm2java
    */
    public class TbPolygonPoint  implements java.io.Serializable {


        private long biPolygonPointId;
        private TbPolygonLoadFile tbPolygonLoadFile;
        private int isuperGroupId;
        private int igroupId;
        private BigDecimal dcLatitude;
        private BigDecimal dcLongitude;
        private Set<TbPolygonHasPoints> tbPolygonHasPointses = new HashSet<TbPolygonHasPoints>(0);

        public TbPolygonPoint() {
        }


        public TbPolygonPoint(long biPolygonPointId, int isuperGroupId, int igroupId, BigDecimal dcLatitude, BigDecimal dcLongitude) {
            this.biPolygonPointId = biPolygonPointId;
            this.isuperGroupId = isuperGroupId;
            this.igroupId = igroupId;
            this.dcLatitude = dcLatitude;
            this.dcLongitude = dcLongitude;
        }
        public TbPolygonPoint(long biPolygonPointId, TbPolygonLoadFile tbPolygonLoadFile, int isuperGroupId, int igroupId, BigDecimal dcLatitude, BigDecimal dcLongitude, Set<TbPolygonHasPoints> tbPolygonHasPointses) {
        this.biPolygonPointId = biPolygonPointId;
        this.tbPolygonLoadFile = tbPolygonLoadFile;
        this.isuperGroupId = isuperGroupId;
        this.igroupId = igroupId;
        this.dcLatitude = dcLatitude;
        this.dcLongitude = dcLongitude;
        this.tbPolygonHasPointses = tbPolygonHasPointses;
        }

        public long getBiPolygonPointId() {
            return this.biPolygonPointId;
        }

        public void setBiPolygonPointId(long biPolygonPointId) {
            this.biPolygonPointId = biPolygonPointId;
        }
        public TbPolygonLoadFile getTbPolygonLoadFile() {
            return this.tbPolygonLoadFile;
        }

        public void setTbPolygonLoadFile(TbPolygonLoadFile tbPolygonLoadFile) {
            this.tbPolygonLoadFile = tbPolygonLoadFile;
        }
        public int getIsuperGroupId() {
            return this.isuperGroupId;
        }

        public void setIsuperGroupId(int isuperGroupId) {
            this.isuperGroupId = isuperGroupId;
        }
        public int getIgroupId() {
            return this.igroupId;
        }

        public void setIgroupId(int igroupId) {
            this.igroupId = igroupId;
        }
        public BigDecimal getDcLatitude() {
            return this.dcLatitude;
        }

        public void setDcLatitude(BigDecimal dcLatitude) {
            this.dcLatitude = dcLatitude;
        }
        public BigDecimal getDcLongitude() {
            return this.dcLongitude;
        }

        public void setDcLongitude(BigDecimal dcLongitude) {
            this.dcLongitude = dcLongitude;
        }
        public Set<TbPolygonHasPoints> getTbPolygonHasPointses() {
            return this.tbPolygonHasPointses;
        }

        public void setTbPolygonHasPointses(Set<TbPolygonHasPoints> tbPolygonHasPointses) {
            this.tbPolygonHasPointses = tbPolygonHasPointses;
        }
    }

1 个答案:

答案 0 :(得分:10)

我在这里找到了答案:

http://dertompson.com/2008/01/03/bigdecimal-and-jdbc-since-java-5-0-2/

显然,转换过程会将double转换为一个字符串,该字符串可能包含十进制数后面的过多值。然后将其转换为BigDecimal。此转换过程是错误的来源。

我通过使用decimalFormat()来保留我想要的有效位数并在新的BigDecimal()转换中使用该字符串来解决这个问题。


edejong 2014-08-20:旧网址已死,重写为新网址