postgresql Hibernate 数据库特定列定义值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1944660/
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
Hibernate database specific columnDefinition values
提问by user1946784
the problem is as follows: We're using hibernate with annotations as O/R Mapper.
问题如下:我们使用带有注释的 hibernate 作为 O/R Mapper。
Some @Column annotations look like:
一些@Column 注释看起来像:
@Column(columnDefinition = "longblob", name = "binaryData", nullable = true)
@Column(columnDefinition = "longblob", name = "binaryData", nullable = true)
or
或者
@Column(columnDefinition = "mediumtext", name = "remark", nullable = true)
@Column(columnDefinition = "mediumtext", name = "remark", nullable = true)
with the columnDefinition attributes being mysql specific
columnDefinition 属性是特定于 mysql 的
on postgres for example, the columnDefinition values should be "bytea" and "varchar(999999)"
例如,在 postgres 上, columnDefinition 值应该是“bytea”和“varchar(999999)”
and on oracle probably something else.
在 oracle 上可能还有别的东西。
Problems arise currently at the time of Schema Export, e.g. when creating the DDL statements.
当前在模式导出时出现问题,例如在创建 DDL 语句时。
Possible workarounds that I can think of are - Hack some JDBC driver that does a text replace (e.g. longblob->bytea) for the DDL statements. That's ugly but will work somehow - Use hibernate xml configuration instead of annotations. That will probably work but I prefer annotations
我能想到的可能解决方法是 - 破解一些 JDBC 驱动程序,为 DDL 语句执行文本替换(例如 longblob->bytea)。这很难看,但会以某种方式工作 - 使用 hibernate xml 配置而不是注释。这可能会奏效,但我更喜欢注释
Does anybody know any alternatives? Hibernate specific workarounds are ok, e.g. if the columnDefinition attribute can contain dialect specific values like
有人知道任何替代方案吗?Hibernate 特定的解决方法是可以的,例如,如果 columnDefinition 属性可以包含方言特定的值,如
@Column(columnDefinition = "mysql->mediumtext, postgres->varchar(999999)", name = "remark", nullable = true)
@Column(columnDefinition = "mysql->mediumtext, postgres->varchar(999999)", name = "remark", nullable = true)
Thanks Holger
谢谢霍尔格
采纳答案by Bozho
Why don't you use the database-agnostic annotations like:
为什么不使用与数据库无关的注释,例如:
@Lob
(on abyte[]
or aString
property)@Column(length=90000)
(on aString
property)
@Lob
(在一个byte[]
或一个String
属性上)@Column(length=90000)
(在String
财产上)
and see what columns will be generated in the database. They will most likely be of the types you want them to be.
并查看将在数据库中生成哪些列。它们很可能是您希望它们成为的类型。
回答by KLE
Some ideas:
一些想法:
- Use annotation in general, but overload them from Xmlin the case where they are specific to your database. Then you can have one configuration file specific to your database.
- Use java Constants in your annotations(they have to be compile-time constants, so you are limited). You can have several sets of Java constants, and point toward the one you want to export to. (Beware, when you point toward another constant, you have to recompile everything.)
- I have also used the dialect to switch some code in my Configuration class. The configuration class receives all data (from annotations or xml), and can then postprocess it.
For example, I have changed the concatenation symbol from '||' on Oracle to '+' on SqlServer.
This is conveniently done at runtime :-)
- 通常使用注释,但在它们特定于您的数据库的情况下从 Xml 重载它们。然后,您可以拥有一个特定于您的数据库的配置文件。
- 在您的注释中使用 java 常量(它们必须是编译时常量,因此您受到限制)。您可以拥有多组 Java 常量,并指向要导出到的一组。(请注意,当您指向另一个常量时,您必须重新编译所有内容。)
- 我还使用方言在我的 Configuration 类中切换了一些代码。配置类接收所有数据(来自注释或 xml),然后可以对其进行后处理。
例如,我已将连接符号从 '||' 更改为 在 Oracle 上为 '+' 在 SqlServer 上。
这在运行时很方便:-)