简体   繁体   中英

Error converting data type nvarchar to decimal error when updating hibernate object

I have a JSF application using hibernate.
I load a set of points for a polygon and want to update the points.

I update the point information, but when I attempt to commit the changes, I get the following error:

    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)

I am confused, since I do not have any character information in the object I am attempting to update.

Does anyone know why I get this error and how can I resolve it? Thanks.

Here is the update code. I get the error when attempting to do the commit:

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

Here is the java code for the object.

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

I found the answer here:

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

Apparently the conversion process converts the double to a string that may contain too many values behind the decimal. That is then converted to the BigDecimal. This conversion process is where the error comes from.

I solved this by using decimalFormat() to retain the number of significant digits I wanted and using that string in the new BigDecimal() conversion.


edejong 2014-08-20: Old url was dead, rewrote to new url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM