JBoss6 JPA: Entity with @Lob results in a GenericJDBCException

I have a simple EntityBean with @Lob annotation. If I delete this annotation, I will not get errors on JBossAS 6.0.0.Final and MySQL5. But if I annotate it using @Lob (because mt contains in my case about 100-500 characters), I get errors in my test environment if I save the object.

  • without @Lob : mt displayed in VARCHAR
  • with @Lob : mt displayed in LONGTEXT (this is what I want but get errors)

This is my essence:

 @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 is here:

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

I really don't know why I am getting this (reproducible) error. The environment seems to be in order, many other tests passed and even work without the @Lob annotation.

This question is related to JPA: how to @Lob string in a database field, enter MYSQL text , where using @Lob for JPA / MySQL is the accepted answer.

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

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

Update 3 . I upgraded MySQL to mysql-5.5.17-osx10.6-x86_64 and connector to mysql-connector-java-5.1.18 . All the same mistakes.

+6
source share
2 answers

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

EDIT:

In addition, you can always set the length for the maximum value of your database, as well as determine the type of column with the columndefinition parameter, depending on your needs.

For example, if you are 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 (~ 16 MB, ~ 5.5 million encoded UTF-8 characters)
  • LONGTEXT: 4,294,967,295 bytes (~ 4 GB, ~ 1.4 billion characters with UTF-8 encoding).

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

+4
source

Another way that worked for me is updating the jBoss startup configuration using

 -Dhibernate.jdbc.use_streams_for_binary=true 

I am running jBoss6 with MySQL5

+2
source

Source: https://habr.com/ru/post/899793/