Hibernate postgresql/hsqldb TEXT 列不兼容问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4213782/
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 postgresql/hsqldb TEXT column incompatibility problem
提问by Nemanja
I have a problem using Hibernate and PostgreSQL for production and HSQLDB for testing.
I am using top-down approach letting Hibernate create database schema.
I am also using annotations; mapping part of hibernate.cfg.xml only contains lines like<mapping class="package.subpackage.ClassName" />
Hibernate defaults String variables to character varying(255) on PostgreSQL which is not sufficient for me in some cases, so I have to redefine some columns manually using @Column(columnDefinition = "TEXT")
.
But, TEXT type is invalid for HSQLDB, so those tables can not be created.
Can anyone help to solve this?
我在使用 Hibernate 和 PostgreSQL 进行生产以及使用 HSQLDB 进行测试时遇到了问题。
我使用自上而下的方法让 Hibernate 创建数据库模式。
我也在使用注释;hibernate.cfg.xml 的映射部分只包含像<mapping class="package.subpackage.ClassName" />
Hibernate 默认字符串变量到 PostgreSQL 上的字符变化(255)这样的行,这在某些情况下对我来说是不够的,所以我必须使用@Column(columnDefinition = "TEXT")
.
但是,TEXT 类型对于 HSQLDB 是无效的,因此无法创建这些表。
任何人都可以帮助解决这个问题吗?
采纳答案by Don Roby
The easiest way to deal with this specific issue is probably to not use the columnDefinition at all and instead to explicitly specify the column length with (for example)
处理此特定问题的最简单方法可能是根本不使用 columnDefinition,而是使用(例如)显式指定列长度
@Column(length=10000)
It might also be that you could instead map it with @Lob(type = LobType.CLOB)
也可能是您可以使用 @Lob(type = LobType.CLOB) 来映射它
but I'm not sure that is supported properly in HSQLDB. In Postgres it should give you your TEXT type.
但我不确定在 HSQLDB 中是否正确支持。在 Postgres 中,它应该为您提供 TEXT 类型。
回答by G. Demecki
Agree with @fredt. TEXT data type isn't standard SQL type, but extension that some engine supports.
同意@fredt。TEXT 数据类型不是标准的 SQL 类型,而是某些引擎支持的扩展。
To enable PostgreSQL compatibility modeuse sql.syntax_pgs=true
in your connection parameters.
为了让PostgreSQL的兼容模式下使用sql.syntax_pgs=true
的连接参数。
回答by fredt
HSQLDB 2.1 and later has a PostgreSQL compatibility mode and supports the TEXT data type in this mode.
HSQLDB 2.1 及更高版本具有 PostgreSQL 兼容模式,并在该模式下支持 TEXT 数据类型。
回答by terrance.a.snyder
To get H2 to work in compatability mode with PostgreSQL (useful for junit testing).
使 H2 在与 PostgreSQL 兼容的模式下工作(对 junit 测试很有用)。
# JDBC Driver
jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:play;MODE=PostgreSQL;TRACE_LEVEL_SYSTEM_OUT=2;DB_CLOSE_DELAY=-1;IGNORECASE=TRUE;INIT=CREATE TABLE IF NOT EXISTS PG_CLASS (RELNAME text, RELKIND text);
jdbc.username=sa
jdbc.password=
# general hibernate options
hibernate.database=h2
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
The create table PG_CLASS is required to allow Hibernate/JPA to correctly function. But other than that - pretty seamless.
创建表 PG_CLASS 需要允许 Hibernate/JPA 正确运行。但除此之外 - 非常无缝。
回答by Sam
Yes, just try on blow to make HSQLDB run in PostgreSQL compatibility mode.
是的,试试blow让HSQLDB在PostgreSQL兼容模式下运行。
jdbc.url=jdbc:h2:mem:mydb;sql.syntax_pgs=true
回答by Szymon Lipiński
Yes, you have a really bigproblem.
是的,你有一个非常大的问题。
DON'T USE ONE DATABASE ENGINE FOR TESTING, AND ANOTHER FOR PRODUCTION.
不要将一个数据库引擎用于测试,而另一个用于生产。
You can hit upon problems you've never dreamed about.
您可以遇到您从未梦想过的问题。