JBoss6 JPA:带有 @Lob 的实体导致 GenericJDBCException

发布于 2024-12-11 08:33:19 字数 1622 浏览 0 评论 0原文

我有一个带有 @Lob 注释的简单 EntityBean。如果我删除这个注释,我在 JBossAS 6.0.0.Final 和 MySQL5 上不会出现错误。但是,如果我用 @Lob 对其进行注释(因为在我的情况下,mt 包含大约 100 到 5000 个字符),如果我保留该实体,我会在测试环境中收到错误。

  • 没有 @Lobmt 映射到 VARCHAR
  • 使用 @Lobmt 映射到 LONGTEXT (这就是我想要,但我收到错误)

这是我的实体:

@Entity
@Table(name = "Description")
public class Description implements Serializable
{ 
  public static final long serialVersionUID=1;

  @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;  

  @Lob
  private String mt;
}  // ... getter/setter

错误在这里:

...
Caused by: org.hibernate.exception.GenericJDBCException: could not insert
   [my.Description]
...
Caused by: java.sql.SQLException: Connection is not associated with a managed
     connection.org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6@3e4dd
...

我真的不知道为什么我收到这个(可重现的)错误。环境似乎没问题,许多其他测试都通过了,甚至在没有 @Lob 注解的情况下也能工作。

这个问题与 JPA: how do I persist a String into a database field, type MYSQL Text 相关,其中使用 @Lob for JPA/MySQL 是可接受的答案。

更新 1 上述错误是操作系统特定的。在 W7 机器上,我对 @Lob 没有任何问题,而 OSX Lion 总是出现错误。我会尝试更新MySQL和驱动程序。

更新 2 Kimi 使用 @Column(columnDefinition = "longtext") 提出的解决方法即使在 OSX 上也能正常工作。在这两种情况下,MySQL 创建相同的列:LONGTEXT。

更新 3 我将 MySQL 更新为 mysql-5.5.17-osx10.6-x86_64 并将连接器更新为 mysql-connector-java-5.1.18 >。还是同样的错误。

I have a simple EntityBean with a @Lob annotation. If I delete this annotation I get no errors on JBossAS 6.0.0.Final and MySQL5. But if I annotate it with @Lob (because mt contains about 100 to 5000 characters in my case) I get errors in my testing environment if I persist the entity.

  • without @Lob: mt is mapped to VARCHAR
  • with @Lob: mt is mapped to LONGTEXT (this is what I want, but I get errors)

This my entity:

@Entity
@Table(name = "Description")
public class Description implements Serializable
{ 
  public static final long serialVersionUID=1;

  @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;  

  @Lob
  private String mt;
}  // ... getter/setter

The error are here:

...
Caused by: org.hibernate.exception.GenericJDBCException: could not insert
   [my.Description]
...
Caused by: java.sql.SQLException: Connection is not associated with a managed
     connection.org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6@3e4dd
...

I really don't know why I get this (reproduceable) error. The environment seems to be ok, many other tests are passed and it even works without the @Lob annotation.

This question is related to JPA: how do I persist a String into a database field, type MYSQL Text, where the usage of @Lob for JPA/MySQL is the accepted answer.

Update 1 The error above is OS specific. On a W7 machine I have no problems with @Lob, with OSX Lion always the error. I will try to update MySQL and the driver.

Update 2 The proposed workaround by Kimi with @Column(columnDefinition = "longtext") works fine, even on OSX. In both cases MySQL creates the same column: LONGTEXT.

Update 3 I updated MySQL to mysql-5.5.17-osx10.6-x86_64 and the connector to mysql-connector-java-5.1.18. Still the same error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神魇的王 2024-12-18 08:33:19

没有需要使用@Lob注释字符串属性。只需使用 @Column(length = 10000) 设置列长度。

编辑:

此外,您始终可以将长度设置为数据库特定的最大值,并使用 columndefinition 设置来定义列类型,以适合您的需要。

例如,如果您使用 MySQL 5.0.3 或更高版本,则每种数据类型可以存储的最大数据量如下:

  • VARCHAR:65,535 字节(~64Kb,21,844 个 UTF-8 编码字符)
  • TEXT:65,535字节(~64Kb,21,844 个 UTF-8 编码字符)
  • MEDIUMTEXT:16,777,215字节(约 16Mb,约 550 万 UTF-8 编码字符)
  • 长文本:4,294,967,295 字节(约 4GB,约 14 亿 UTF-8 编码字符)。

据我了解,@Lob 只是根据底层数据库设置列类型和长度。

There is no need to annotate a String property with @Lob. Just set the column length with @Column(length = 10000).

EDIT:

Moreover, you can always set the length to your database specific maximum value, as well as define the column type with the columndefinition setting to whichever suits your needs.

For example, if you're using MySQL 5.0.3 or later, the maximum amount of data that can be stored in each data type is as follows:

  • VARCHAR: 65,535 bytes (~64Kb, 21,844 UTF-8 encoded characters)
  • TEXT: 65,535 bytes (~64Kb, 21,844 UTF-8 encoded characters)
  • MEDIUMTEXT: 16,777,215 bytes (~16Mb, ~5.5 million UTF-8 encoded characters)
  • LONGTEXT: 4,294,967,295 bytes (~4GB, ~1.4 billion UTF-8 encoded characters).

As i understand it, @Lob just sets the column type and length depending on the underlying database.

杀手六號 2024-12-18 08:33:19

对我有用的另一种方法是更新 jBoss 的启动配置

-Dhibernate.jdbc.use_streams_for_binary=true

我正在运行 jBoss6 和 MySQL5

Another way that worked for me is to update launch configuration of jBoss with

-Dhibernate.jdbc.use_streams_for_binary=true

I'm running jBoss6 with MySQL5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文