简体   繁体   中英

Persisting Serializable Objects in Hibernate

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM