简体   繁体   中英

What is the best way of store arrays with Hibernate?

I want to store an double[] array as Blob in a database with Hibernate. But when I marked the property as Lob , something like this:

@Entity
class MyEntity {
    @Lob
    double[] data;
}  

I got the error D cannot be cast to java.sql.Blob . (Wherein for the double[][] array this is works...) So, my question is how to persis dobule[] array with Hibernate?

UPD I forgot the main thing, sorry... The array is big enough, some thousand of values (more specific 5000-20000 values). And I want use the Blob compression option from H2 database. So, I need the Lob , but not List .

UPD2 Another detail, it is read-only array.

In my opinion, using @ElementCollection for that and letting Hibernate deal with optimizations is best:

@Entity
class MyEntity {
    @ElementCollection
    List<Double> data;
}  

https://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-collection

@Lob annotation doesn't support double[] type. Types supported by @Lob are:

  • java.sql.Clob, Character[], char[] and java.lang.String -> persisted in database as Clob
  • java.sql.Blob, Byte[], byte[] and serializable types -> persisted in database as Blob

What you could do is create a custom serializable class and use it instead

@Entity
class MyEntity {
    @Lob
    DoubleArray data;
}  



public class DoubleArray implements Serializable {

    private double[] data;

    //constructor, getter and setter

}

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