Java JPA - 如何在 DDL 中将字符串列设置为 varchar(max)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16414215/
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
JPA - How to set string column to varchar(max) in DDL
提问by Tobb
With JPA, DDL-generation for the attribute:
使用JPA,属性的 DDL 生成:
@Column
final String someString;
will be someString varchar(255) null
将会 someString varchar(255) null
@Column(length = 1337)
final String someString;
will yield someString varchar(1337) null
.
会屈服someString varchar(1337) null
。
But how can I get it to produce someString varchar(max) null
?
但是我怎样才能让它生产someString varchar(max) null
呢?
Is it possible using the length
-attribute, or do I need to use the columnDefinition
-attribute?
是否可以使用length
-attribute,或者我是否需要使用columnDefinition
-attribute?
采纳答案by Tobb
Some months have passed, new knowledge acquired, so I'll answer my own question:
几个月过去了,获得了新知识,所以我将回答我自己的问题:
@Lob
@Column
final String someString;
yields the most correct result. With the version of hbm2ddl
I'm using, this will be transformed to the type text
with SqlServerDialect
. Since varchar(max)
is the replacement for text
in newer versions of SQL Server, hopefully, newer versions of hbm2ddl
will yield varchar(max)
instead of text
for this type of mapping (I'm stuck at a quite dated version of Hibernate at the moment..)
产生最正确的结果。对于hbm2ddl
我正在使用的版本,这将转换为text
带有SqlServerDialect
. 由于varchar(max)
是text
更新版本的SQL Server的替代品,希望新版本的hbm2ddl
将产生varchar(max)
而不是text
这种类型的映射(我目前停留在一个相当过时的 Hibernate 版本中..)
回答by DataNucleus
Since length
is defined in the JPA spec and javadocs as int
type and max
is not an int
then it's safe to assume that you're consigned to the columnDefinition
datastore-dependent route. But then varchar(max)
is datastore-dependent anyway.
由于length
在 JPA 规范和 javadocs 中定义为int
类型并且max
不是类型,int
因此可以安全地假设您被委托到columnDefinition
依赖于数据存储的路由。但varchar(max)
无论如何都是依赖于数据存储的。
回答by Anudeep Sharma
Use @Size(max = 1337)
. It does generate varchar(1337)
使用@Size(max = 1337)
. 它确实产生varchar(1337)
回答by cm_mehdi
You Can Use this Code -
您可以使用此代码 -
Model Code:
型号代码:
@NotNull
@Length(max = 7)
@Column(name = "Gender")
private String gender;
SQL Output is like-
SQL输出就像-
> gender varchar(7)