Java Oracle Char 类型和 Hibernate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3529897/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:34:42  来源:igfitidea点击:

Oracle Char type and Hibernate

javahibernateoracle10g

提问by Kiva

I have a oracle table which contains char(n) type for several columns. I use hibernate tools to create entities objets and this tool map char type in String.

我有一个 oracle 表,其中包含几列的 char(n) 类型。我使用休眠工具来创建实体对象,这个工具在字符串中映射字符类型。

But when I deploy my application, I get an error because Hibernate wait a varchar2 type and not a char type:

但是当我部署我的应用程序时,我收到一个错误,因为 Hibernate 等待 varchar2 类型而不是 char 类型:

Wrong column type in ARBOR.CMF for column CHG_WHO. Found: char, expected: varchar2(30 char)

What kind of java type must I use to map char(n) type in an entity ?

我必须使用哪种 java 类型来映射实体中的 char(n) 类型?

Thanks.

谢谢。

采纳答案by stark

There's some useful information here on this blog entry.

在此博客条目上有一些有用的信息

Essentially you need to make your hibernate configuration more specific, as it's assuming a default String mapping to varchar2(30).

本质上,您需要使您的休眠配置更加具体,因为它假设默认字符串映射到 varchar2(30)。

You can try using the database-agnostic annotation of @Column(length=N) (where N is the actual length of your column in the database). If that doesn't work, try using the @Column(columnDefinition = "char") approach.

您可以尝试使用 @Column(length=N) 的与数据库无关的注释(其中 N 是数据库中列的实际长度)。如果这不起作用,请尝试使用 @Column(columnDefinition = "char") 方法。

回答by Jim Tough

Doesn't work - fails Hibernate schema validation:

不起作用 - 休眠模式验证失败:

@Column(name="ENABLED_FLAG", length=1)
private String enabledFlag;

Does work, since the columnDefinitionattribute tells Hibernate not to default to VARCHAR2as the column type, and to use CHARinstead:

确实有效,因为该columnDefinition属性告诉 Hibernate 不要默认VARCHAR2为列类型,CHAR而是使用:

@Column(name="ENABLED_FLAG", length=1, columnDefinition="CHAR")
private String enabledFlag;

The column in the database is defined as:

数据库中的列定义为:

ENABLED_FLAG CHAR(1 BYTE)