简体   繁体   English

hibernate 中的存储列表为可序列化 object

[英]Store List in hibernate as Serializable object

Normally we store a List in database by the hibernate mapping:通常我们通过 hibernate 映射在数据库中存储一个列表:

<list name="userItems" cascade="all" lazy="false">
    <key column="user_date_id"/>
    <index column="idx"/>
    <one-to-many class="UserItem"/>
</list>

Is there any other way?还有其他方法吗? Can we store it as a Seializable object?我们可以将其存储为 Seializable object 吗? Like:喜欢:

<property name="list" column="list" type="serializable" />

I use Hibernate Annotiations, and the trick here is to define your field as a List implementation like ArrayList .我使用 Hibernate 注释,这里的技巧是将您的字段定义为像ArrayList这样的List实现。 Then (like any other class implementing Serializable - i guess) Hibernate stores it as a bytea in the database.然后(就像任何其他实现可Serializable的 class 一样 - 我猜) bytea将其作为字节存储在数据库中。

Of course the list should only contain elements that implement Serializable .当然,列表应该只包含实现Serializable的元素。

I'm not sure you can directly serialize a list but if not you might create a wrapper object for the list and declare it as large object (don't know the xml variant, but it's the @Lob annotation).我不确定您是否可以直接序列化列表,但如果不能,您可以为列表创建一个包装器 object 并将其声明为大 object(不知道 Z0F635D0E0F3874FFF8B581C132E6C7A 变体,但它是 @ @Lob注释的变体)。 This should automatically be serialized then, since its not a string and thus not a clob but rather a blob.这应该自动序列化,因为它不是字符串,因此不是 clob,而是 blob。

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

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