更新hibernate对象时,将数据类型nvarchar转换为十进制错误时出错 [英] Error converting data type nvarchar to decimal error when updating hibernate object

查看:236
本文介绍了更新hibernate对象时,将数据类型nvarchar转换为十进制错误时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用hibernate的JSF应用程序。

我为一个多边形加载一组点,并且想要更新点。



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

 警告:SQL错误:8114 ,SQLState:S0005 
SEVERE:将数据类型nvarchar转换为十进制时出错。
SEVERE:无法使数据库状态与会话
同步org.hibernate.exception.SQLGrammarException:无法更新:[hibernate.TbPolygonPoint#937]
位于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;
尝试
{
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);
尝试
{
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;
}
返回统计;
}

这是对象的java代码。

  import java.math.BigDecimal; 
import java.util.HashSet;
import java.util.Set;
$ b $ / **
* TbPolygonPoint由hbm2java生成
* /
公共类TbPolygonPoint实现java.io.Serializable {


私人长biPolygonPointId;
private TbPolygonLoadFile tbPolygonLoadFile;
private int isuperGroupId;
private int igroupId;
private BigDecimal dcLatitude;
私人BigDecimal dcLongitude;
private Set< TbPolygonHasPoints> tbPolygonHasPoints = new HashSet< TbPolygonHasPoints>(0);
$ b $ 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; (bPolygonLoadFile 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(){
返回this.tbPolygonHasPointses;
}

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


解决方案

I在这里找到答案:

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



显然,转换过程将double转换为可能包含十进制后面太多值的字符串。然后将其转换为BigDecimal。这个转换过程就是错误来自的地方。



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






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


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

这篇关于更新hibernate对象时,将数据类型nvarchar转换为十进制错误时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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