Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JBoss6 JPA: Entity with @Lob results in GenericJDBCException

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.

like image 989
Thor Avatar asked Oct 21 '11 14:10

Thor


1 Answers

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.

like image 95
Kimi Avatar answered Sep 23 '22 15:09

Kimi