Java 如何告诉 JPA 首选的数据类型

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

How to Tell JPA the prefered DataType

javadatabasejpatypeseclipselink

提问by Marcel Menz

If I use JPA (EclipseLink) to create tables a String type results in a varchar2(255). How could I tell JPA (via Annotation) to create a varchar2(20) attribute.

如果我使用 JPA (EclipseLink) 创建表,则 String 类型会导致 varchar2(255)。我怎么能告诉 JPA(通过注释)创建一个 varchar2(20) 属性。

If I have a List JPA creates a BLOB(4000) but I would like a varchar2 (my serialized object's string is short)

如果我有一个 List JPA 创建一个 BLOB(4000) 但我想要一个 varchar2(我的序列化对象的字符串很短)

How is this possible? Do I have to do it by hand?

这怎么可能?我必须手动完成吗?

采纳答案by William

You need to use the columnDefinition property of the @Column annotation. i.e.

您需要使用 @Column 注释的 columnDefinition 属性。IE

@Column(columnDefinition="varchar2(20)")

回答by Vladimir Ivanov

@Column(name = "doc_number", columnDefinition = "varchar2(20)")

Please, try that.

请试试那个。

回答by Pascal Thivent

If I use JPA (EclipseLink) to create tables a String type results in a varchar2(255). How could I tell JPA (via Annotation) to create a varchar2(20) attribute.

如果我使用 JPA (EclipseLink) 创建表,则 String 类型会导致 varchar2(255)。我怎么能告诉 JPA(通过注释)创建一个 varchar2(20) 属性。

Using the columnDefinitioncan break portability from one database to another. For a string-valued column, prefer using the lengthelement (which defaults to 255):

使用columnDefinition可以破坏从一个数据库到另一个数据库的可移植性。对于字符串值列,更喜欢使用length元素(默认为 255):

@Column(length=20)
String someString;

回答by Steven Benitez

You can set the lengthon your @Columnannotation, as such:

您可以length@Column注释上设置,如下所示:

@Column(length = 20)

Note that length is only for text columns. For numeric, you can use precisionand scale.

请注意,长度仅适用于文本列。对于数字,您可以使用precisionscale