[英]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支持的类型有:
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.