java HibernateException:错误的列类型:x,预期:y 在 Sybase 上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26774193/
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
HibernateException: Wrong column type:x, expected: y on Sybase
提问by callie16
I don't have much background on the technologies below so any help would be appreciated. Please feel free to ask questions if something's not clear.
我对以下技术没有太多背景,因此将不胜感激。如果有什么不清楚的地方,请随时提出问题。
I'm currently working on a migration project wherein we're updating a number of technologies including:
我目前正在开展一个迁移项目,其中我们正在更新多项技术,包括:
- Sybase- from 12.x to 15.7
- JConnect- from several versions to 7.0.7_SP130
- Sybase- 从 12.x 到 15.7
- JConnect- 从多个版本到 7.0.7_SP130
Now, our apps are deployed on to JBoss 4.xand use Hibernate 3.2.4.sp1. In the old DB, we have a number of custom datatypes that look something like this (sorry, would have loved to put this in table format, but I don't know how to here...) :
现在,我们的应用程序部署到JBoss 4.x并使用Hibernate 3.2.4.sp1。在旧数据库中,我们有许多自定义数据类型,它们看起来像这样(抱歉,很想将其放入表格格式,但我不知道如何在这里...):
- Custom datatype: TYPE1, Base datatype: image
- Custom datatype: TYPE2, Base datatype: tinyint
- 自定义数据类型:TYPE1,基本数据类型:图像
- 自定义数据类型:TYPE2,基本数据类型:tinyint
Some columns in our DDL use the custom datatypes:
我们 DDL 中的一些列使用自定义数据类型:
CREATE TABLE y (
our_column_name TYPE1,
...
);
When the server is started up, I get the following error message:
当服务器启动时,我收到以下错误消息:
... javax.persistence.PersistenceException: org.hibernate.HibernateException: Wrong column type: our_column_name, expected: image
... javax.persistence.PersistenceException:org.hibernate.HibernateException:错误的列类型:our_column_name,预期:图像
which is not what happens in the old setup. Do take note that of all the custom datatypes we've defined, only those with imageand tinyinthave issues; the others are recognized and throw no error.
这不是旧设置中发生的情况。请注意,在我们定义的所有自定义数据类型中,只有带有image和tinyint 的数据类型有问题;其他人被识别并且不会出错。
We've run a trace on the JConnect driver and it appears that it's retrieving the correct datatype, which is why I'm now focusing on the HibernateException. I've seen a couple of posts similar to this one (occurring on different DBs), but basically, they gave similar workarounds:
我们在 JConnect 驱动程序上运行了一个跟踪,它似乎正在检索正确的数据类型,这就是我现在关注 HibernateException 的原因。我看过几篇类似于这个的帖子(发生在不同的数据库上),但基本上,他们给出了类似的解决方法:
One site like this one, it's suggested that we alter the DB table's column to use the base datatype instead of the custom datatype. We've done this by changing our_column_name's datatype to
image
. Once this was done and the server was restarted, the error goes away. However, it does not explain whyor what caused the issue.Thissuggested the use of JPA annotations'
@Column(columnDefinition='image')
. We've tried this as well, but it doesn't seem to have any effect on the startup (i.e. error still occurred).In the same link as #2, it was suggested that the SQL Dialect be extended. However, I don't think this is feasible - only 2 custom datatypes (image and tinyint) seem to be causing the problems on our end so this may be overkill.
This sitesuggests the removal of
hibernate.hbm2ddl.auto=validate
from persistence.xml. Have not tried this as we need the validation to be in place.Use the datatype "LOB" instead
像这样的一个站点,建议我们更改 DB 表的列以使用基本数据类型而不是自定义数据类型。我们通过将our_column_name的数据类型更改为
image
. 一旦完成并重新启动服务器,错误就会消失。但是,它没有解释导致问题的原因或原因。这建议使用 JPA 注释'
@Column(columnDefinition='image')
. 我们也试过这个,但它似乎对启动没有任何影响(即仍然发生错误)。在与#2 相同的链接中,建议扩展 SQL 方言。但是,我认为这是不可行的 - 只有 2 种自定义数据类型(image 和 tinyint)似乎在我们这边造成了问题,所以这可能有点矫枉过正。
该站点建议
hibernate.hbm2ddl.auto=validate
从persistence.xml 中删除。还没有尝试过这个,因为我们需要验证到位。改用数据类型“LOB”
I've also tried checking out Hibernate's code since the exception was thrown here - org.hibernate.mapping.Table.validateColumns(Table.java:261)
, which pretty much points to this line:
我也试过检查 Hibernate 的代码,因为这里抛出了异常 - org.hibernate.mapping.Table.validateColumns(Table.java:261)
,它几乎指向这一行:
boolean typesMatch = (col.getSqlType(dialect, mapping).startsWith(columnInfo.getTypeName().toLowerCase())) || (columnInfo.getTypeCode() == col.getSqlTypeCode(mapping));
However, Hibernate's API documentation is a little wanting in details so I'm having some difficulty in tracing this at the moment...
但是,Hibernate 的 API 文档在细节方面有点欠缺,所以我目前在跟踪这个方面遇到了一些困难......
Any ideas on what's causing the exception? If it's working in the old version, was wondering on what changes in Hibernate (or Sybase for that matter) can cause this?
关于导致异常的原因的任何想法?如果它在旧版本中工作,想知道 Hibernate(或 Sybase)中的哪些变化会导致这种情况?
Thisdocument suggests that Hibernate is tested against Sybase 15.7 so I'm at a loss where to continue looking. And would #1 above be the best workaround? If so, any ideas why base types should be used instead of custom datatypes (which would, in essence, render custom datatypes useless...)
该文档表明 Hibernate 已针对 Sybase 15.7 进行了测试,因此我不知道在哪里继续寻找。上面的#1 会是最好的解决方法吗?如果是这样,任何想法为什么应该使用基类型而不是自定义数据类型(本质上,这会使自定义数据类型变得无用......)
Thanks again in advance!
再次提前致谢!
EDIT:
编辑:
Tried the following:
尝试了以下方法:
- If
@Column(columnDefinition='my_custom_datatype')
is used, then the error goes away. - If
hibernate.hbm2ddl.auto=validate
is removed from persistence.xml, the error goes away.
- 如果
@Column(columnDefinition='my_custom_datatype')
使用,则错误消失。 - 如果
hibernate.hbm2ddl.auto=validate
从persistence.xml 中删除,错误就会消失。
I'm really suspecting it's a hibernate issue...
我真的怀疑这是一个休眠问题...
回答by callie16
This is similar in nature to this question on SOi.e. typesMatch
in the code in my query above will be false because the values being returned by col
and columnInfo
are different - one is returning the base datatype, the other is returning the custom user type.
这在本质上类似于SO上的这个问题,即typesMatch
在我上面查询中的代码中将是假的,因为返回的值col
和columnInfo
不同 - 一个返回基本数据类型,另一个返回自定义用户类型。
So, to deal with it, a number of workarounds are possible (and I've mentioned it in my post above) -
因此,要处理它,可以有多种解决方法(我在上面的帖子中已经提到过)-
- Remove
hibernate.hbm2ddl.auto=validate
if you can. - Change the column datatypes directly in DB, which I think is the next fastest approach.
- Use
@Column(columnDefinition='my_custom_datatype')
in the Java class.
hibernate.hbm2ddl.auto=validate
如果可以,请删除。- 直接在 DB 中更改列数据类型,我认为这是下一个最快的方法。
- 使用
@Column(columnDefinition='my_custom_datatype')
Java类。
回答by Renato Felix
If you use @DiscriminatorColumn remember to put there too. Like this:
如果你使用 @DiscriminatorColumn 记得把它也放在那里。像这样:
@DiscriminatorColumn(name = "MyTableName", discriminatorType = DiscriminatorType.STRING, columnDefinition = "char")
and into the attribute
并进入属性
@Column(name="MyColumnName",nullable = true, insertable = true, updatable = true, length = 1, columnDefinition = "char")
private String type;