Java 更新休眠对象时将数据类型 nvarchar 转换为十进制错误时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9723072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error converting data type nvarchar to decimal error when updating hibernate object
提问by Burferd
I have a JSF application using hibernate.
I load a set of points for a polygon and want to update the points.
我有一个使用休眠的 JSF 应用程序。
我为多边形加载了一组点并想要更新这些点。
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.
这是对象的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;
}
}
回答by Burferd
I found the answer here:
我在这里找到了答案:
http://dertompson.com/2008/01/03/bigdecimal-and-jdbc-since-java-5-0-2/
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.
显然,转换过程将 double 转换为一个字符串,该字符串可能在十进制后面包含太多值。然后将其转换为 BigDecimal。这个转换过程是错误的来源。
I solved this by using decimalFormat() to retain the number of significant digits I wanted and using that string in the new BigDecimal() conversion.
我通过使用 decimalFormat() 保留我想要的有效数字的数量并在新的 BigDecimal() 转换中使用该字符串解决了这个问题。
edejong 2014-08-20: Old url was dead, rewrote to new url
edejong 2014-08-20:旧网址已死,改写为新网址