Tapas Bose
Tapas Bose

Reputation: 29816

Store List in hibernate as Serializable object

Normally we store a List in database by the 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" />

Upvotes: 2

Views: 1873

Answers (2)

alfonx
alfonx

Reputation: 7216

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.

Upvotes: 3

Thomas
Thomas

Reputation: 88747

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.

Upvotes: 1

Related Questions