-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Persisting a collection of enums using hibernate
PostPosted: Wed Jul 25, 2007 10:42 am 
Newbie

Joined: Wed Jul 25, 2007 10:23 am
Posts: 1
Hello there,

I have the requirement to persist a collection of enums using hibernate.
There seems to be a raft of information on how to persist a single enum by declaring a user type and then associating genericised enums to the user type. This, I believe, is possible as the <property> element in the mapping file can be used in an 'open' fashion, which then allows the the use of the <type> element, such as:

<property name="articleType" column="ARTICLE_TYPE" not-null="false">
<type name="uk.co.smg.paymag.util.hibernate.EnumUserType">
<param name="enumClassName">uk.co.smg.paymag.model.ArticleType</param>
</type>
</property>

However, I am struggling to replicate this 2 stage mapping declaration process using a collection, specifically an array.
The issue, as I see it, is that when declaring my association type (one-to-many) within the <array> collection element, the <one-to-many> element has been declared as 'empty' and as such I only get one opportunity to declare my usertype in the 'class' attribute:

<array name="jobTypes" cascade="save-update,persist">
<key column="JOB_ALERT_JOB_TYPE"/>
<list-index column="id"/>
<one-to-many class="uk.co.smg.paymag.util.jobs.JobType"/>
</array>

So, basically I would like to know if anyone has found a way to persist a collection of enums using hibernate.

Any help much appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 11:02 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You can persist a collection of enums by using <element> inside the <array> mapping:
Code:
  <array name="jobTypes" cascade="save-update,persist">
    <key column="JOB_ALERT_JOB_TYPE"/>
    <list-index column="jobTypesOrder"/>
    <element column="size" type="test.adam.enumcollection.JobType"/>
  </array>


You don't need to provide a hibernate mapping file for the enum type. Hibernate serailizes the enum into a separate table holding the array data. The table name defaults to the array's property name but can be specified by a table attribute in the array element.

Mike


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.