在没有Hibernate注解的情况下纠正PostgreSQL文本类型的JPA注释 [英] Correct JPA Annotation for PostgreSQL's text type without Hibernate Annotations
问题描述
我正在开发一个应用程序,使用:
$ b
我想为某些String属性使用PostgreSQL文本数据类型。据我所知,在JPA这应该是正确的注释,在PostgreSQL中使用文本:
@Entity
公共类产品{
...
@Lob
私有字符串描述;
....
}
当我注解我的实体时,我遇到了这样的错误:
http://www.shredzone.de/cilla/page/299/string-lobs-on-postgresql-with-hibernate-36.html
简而言之:看起来hibernate和jdbc不会携手用于clob / text-types。
所描述的解决方案是可行的: p>
@Entity
public class Product {
...
@Lob
@ Type(type =org.hibernate.type.TextType)
private String description;
...
}
但是这有一个重要的缺点:代码在编译时需要hibernate,这应该是不必要的(这是首先使用JPA的一个原因)。另一种方法是使用列注释,如下所示:
@Entity
public class Product {
...
@Column(columnDefinition = text)
私有字符串描述;
...
}
这很好,但是:
现在我坚持使用具有文本类型(也称为text;)的数据库,并且如果未来将使用另一个数据库,则可以轻松地忽略注释。因此,可能的错误很难找到,因为数据类型是在字符串中定义的,因此在运行时无法找到。
有没有解决方案,容易,我只是看不到它?我非常肯定,我不是唯一一个将JPA与Hibernate和PostgreSQL结合使用的人。所以我有点困惑,我找不到更多这样的问题。
为了完成这个问题,persistence.xml看起来像这样:
<?xml version =1.0encoding =UTF-8?>
< persistence version =1.0
xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/ 2001 / XMLSchema-instance
xsi:schemaLocation =http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/ persistence_1_0.xsd>
< persistence-unit name =entityManager>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< class> com.app.model.Product< / class>
<属性>
< property name =javax.persistence.jdbc.drivervalue =org.postgresql.Driver/>
< property name =javax.persistence.jdbc.url
value =jdbc:postgresql:// localhost:5432 / awesomedb/>
< property name =javax.persistence.jdbc.uservalue =usr/>
< property name =javax.persistence.jdbc.passwordvalue =pwd/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.PostgreSQLDialect/>
< property name =hibernate.jdbc.use_streams_for_binaryvalue =false/>
< property name =hibernate.hbm2ddl.autovalue =create-drop/>
< property name =show_sqlvalue =true/>
< / properties>
< / persistence-unit>
< /余辉>
更新: 这个问题或多或少与这个问题等价,所挑选的答案是在这个问题中描述的第二种方法,我不喜欢由于hibernate运行时依赖:
在Postgresql中存储任意长度的字符串
文本
类型不是SQL标准的一部分,所以没有官方的JPA方式。
但是, text
类型与 varchar
非常相似,但没有长度限制。您可以使用长度
属性 @Column
来提示JPA实现:
@Column(length = 10485760)
private String description;
更新: 10 MiB似乎是<$ c的最大长度$ c> varchar 在postgresql中。 文本
根据无限制的-character.htmlrel =nofollow> documentation :
无论如何,最长的字符串可以存储
约为1 GB。
I'm developing an application using:
- Java 1.7
- JPA (included in javaee-api 7.0)
- Hibernate 4.3.8.Final
- PostgreSQL-JDBC 9.4-1200-jdbc41
- PostgreSQL 9.3.6
And I would like to use the PostgreSQL text datatype for some String attributes. As far as I know, in JPA this should be the correct annotation, to use text in PostgreSQL:
@Entity
public class Product{
...
@Lob
private String description;
....
}
When I annotate my entity like this, I run into errors which look like this: http://www.shredzone.de/cilla/page/299/string-lobs-on-postgresql-with-hibernate-36.html
In short: It seems that hibernate and jdbc go not hand in hand for clob/text-types.
The solution described is working:
@Entity
public class Product{
...
@Lob
@Type(type = "org.hibernate.type.TextType")
private String description;
...
}
But this has a significant downside: The source code needs hibernate at compile time, which should be unnecessary (That's one reason for using JPA in the first place).
Another way is to use the column annotation like this:
@Entity
public class Product{
...
@Column(columnDefinition = "text")
private String description;
...
}
Which works nicely, BUT: Now I'm stuck with databases which have a text type (and is also called text ;) ) and if another database will be used in the future the annotations can be overlooked easily. Thus the possible error can be hard to find, because the datatype is defined in a String and therefore can not be found before runtime.
Is there a solution, which is so easy, I just don't see it? I'm very sure that I'm not the only one using JPA in combination with Hibernate and PostgreSQL. So I'm a little confused that I can't find more questions like this.
Just to complete the question, the persistence.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="entityManager">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.app.model.Product</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:postgresql://localhost:5432/awesomedb" />
<property name="javax.persistence.jdbc.user" value="usr" />
<property name="javax.persistence.jdbc.password" value="pwd" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.jdbc.use_streams_for_binary" value="false" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
UPDATE:
This problem is more or less equivalent with this question, the picked answer is the second way to do it described in this question, which I don't like due to the hibernate runtime dependency: store strings of arbitrary length in Postgresql
This seems to be kind of related to: https://hibernate.atlassian.net/browse/JPA-48
Since the text
type is not a part of the SQL standard there is no official JPA way I guess.
However, the text
type is quite similar to varchar
, but without the length limit. You can hint the JPA implementation with the length
property of @Column
:
@Column(length=10485760)
private String description;
Update: 10 MiB seems to be the maximum length for varchar
in postgresql. The text
is almost unlimited, according the documentation:
In any case, the longest possible character string that can be stored is about 1 GB.
这篇关于在没有Hibernate注解的情况下纠正PostgreSQL文本类型的JPA注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!