开发者

Store List in hibernate as Serializable object

Normally we store a List in database by t开发者_高级运维he hibernate mapping:

<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? 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. Then (like any other class implementing Serializable - i guess) Hibernate stores it as a bytea in the database.

Of course the list should only contain elements that implement 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). This should automatically be serialized then, since its not a string and thus not a clob but rather a blob.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜