gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:53:28 28 4
gpt4 key购买 nike

我有一个使用 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;
}
}

最佳答案

我在这里找到了答案:

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

显然,转换过程将 double 转换为字符串,该字符串可能包含太多小数点后面的值。然后将其转换为 BigDecimal。这个转换过程就是错误的来源。

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

<小时/>

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

关于java - 更新 hibernate 对象时将数据类型 nvarchar 转换为十进制错误时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9723072/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com