views:

70

answers:

1

I am attempting to persist objects that contain some large Serializable types. I want Hibernate to automatically generate my DDL (using Hibernate annotations). For the most part, this works, but the default database column type used by Hibernate when persisting these types is tinyblob. Unfortunately, this causes crashes when attempting to persist my class, because these types will not fit within the length of tinyblob.

However, if I manually set the type (using @Column(columnDefinition="longblob"), or more portably @Column(length=500000)), it works fine. Is there any way to make the default binary type longblob instead of tinyblob, so that I don't need to manually specify the @Column annotation on each field?

ExampleClass.java:

public class ExampleClass
{
  @Column(columnDefinition="longblob")
  ExampleSerializable ser1;

  @Column(columnDefinition="longblob")
  ExampleSerializable ser2;

  ...
}

ExampleSerializable.java:

public class ExampleSerializable implements java.io.Serializable
{
  // MANY Fields
}

EDIT

Since there seems to be some confusion: annotating each field with @Column(columnDefinition="longblob") (or more portably: @Column(length=500000)), already works. I am looking for a solution that does not require me to annotate each and every field.

+1  A: 

I think (didn't test it) that Hibernate will generate a tinyblob, blob, mediumblob column depending on the column length (respectively 255, 65535, 16777215) which defaults to 255. I would try to specify it (and this would be a portable solution).

Now, if really you want to force things, you'll have to extend the MySQL dialect (but this will harm portability).

Pascal Thivent
Is there a way to specify a default column length? Having to specify the column length on each member is no easier than having to specify `longblob` on each member.
VeeArr
@VeeArr: It's maybe not easier, but it's portable (`longblob` might not exist with another database). Regarding the length of a `Column`, this default is defined by the JPA specification and patching the persistence-api.jar can't be a good advice. I don't mean to be rude, but I think you're expecting too much magic.
Pascal Thivent
Ah, yes, I understood your point that it is more portable (and thus better than my original solution). I was addressing the issue that it didn't solve my problem, though. But thanks for your response!
VeeArr