简体   繁体   English

使用Hibernate的商店数组的最佳方法是什么?

[英]What is the best way of store arrays with Hibernate?

I want to store an double[] array as Blob in a database with Hibernate. 我想将一个double[]数组作为Blob在具有Hibernate的数据库中。 But when I marked the property as Lob , something like this: 但当我将该属性标记为Lob ,这样的事情:

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

I got the error D cannot be cast to java.sql.Blob . 我得到错误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? (其中对于double[][]数组这是有效的...)所以,我的问题是如何使用Hibernate进行persis dobule[]数组?

UPD I forgot the main thing, sorry... The array is big enough, some thousand of values (more specific 5000-20000 values). UPD我忘记了主要的事情,对不起......数组足够大,数千个值(更具体的5000-20000值)。 And I want use the Blob compression option from H2 database. 我想使用H2数据库中的Blob压缩选项。 So, I need the Lob , but not List . 所以,我需要Lob ,但不需要List

UPD2 Another detail, it is read-only array. UPD2另一个细节,它是只读数组。

In my opinion, using @ElementCollection for that and letting Hibernate deal with optimizations is best: 在我看来,使用@ElementCollection并让Hibernate处理优化是最好的:

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

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

@Lob annotation doesn't support double[] type. @Lob注释不支持double []类型。 Types supported by @Lob are: @Lob支持的类型有:

  • java.sql.Clob, Character[], char[] and java.lang.String -> persisted in database as Clob java.sql.Clob,Character [],char []和java.lang.String - >作为Clob保存在数据库中
  • java.sql.Blob, Byte[], byte[] and serializable types -> persisted in database as Blob java.sql.Blob,Byte [],byte []和可序列化类型 - >作为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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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