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.  [ 7 posts ] 
Author Message
 Post subject: Mapping a collection of enums
PostPosted: Sun Feb 19, 2006 11:06 am 
Newbie

Joined: Thu Nov 17, 2005 1:03 pm
Posts: 9
Location: Round Rock, TX
Does anyone have an idea of the easiest way to do the following:

I have a Company class and an enum called CompanyRole. A Company can have one or more CompanyRoles so Id like to declare a IList called Roles in my Company class that is a collection of values form the CompanyRole enum.

In the database I suppose that other than the Company table I'll need a CompanyRoles table that contains an fk column to the Compay table and a CompanyRole int column.

So, I'd like to map the Company Table to the Company class. I'm hoping that I don't need to define an additional class called someting like CompanyRoles that only holds a single CompanyRole vale (remember that CompanyRole is my enum).

Any good ideas there???

Thanks,
Greg


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 19, 2006 9:45 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You need to use UserTypes. There are several wiki pages on the Hibernate2 way of implementing enums using UserTypes. http://www.hibernate.org/172.html is a good place to start. http://www.hibernate.org/37.html lists a few other pages. Check the dates of postings, as quite a few suggestions in those pages use Java5's enums, while others use patterns with Java2.


Top
 Profile  
 
 Post subject: NHibernate mapping
PostPosted: Mon Feb 20, 2006 11:41 am 
Newbie

Joined: Mon Feb 20, 2006 11:38 am
Posts: 2
Outlines different approaches to 1-many mapping to an enum

Works if you have a unique identifier on your many table
<idbag name="companyRoleList" access="field" lazy="false" cascade="none" table="CompanyRole" >
<collection-id column="companyRoleID" type="Guid">
<generator class="guid.comb" />
</collection-id>
<key column="CompanyID" />
<element column="RoleID" type="MyCompany.Domain.Lookups.CompanyRoleType, MyCompany.Domain.Lookups" />
</idbag>

Works but returns an NHib Set which does not support IList
<set name="companyRoleList" access="field" lazy="false" cascade="none" table="CompanyRole" >
<key column="CompanyID" />
<element column="RoleID" type="MyComapny.Domain.Lookups.CompanyRoleType, MyCompany.Domain.Lookups" />
</set>

This is what I ultimately used...
<bag name="companyRoleList" access="field" lazy="false" cascade="none" table="CompanyRole" >
<key column="CompanyID" />
<element column="RoleID" type="MyCompany.Domain.Lookups.CompanyRoleType, MyCompany.Domain.Lookups" />
</bag>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 20, 2006 12:39 pm 
Newbie

Joined: Thu Nov 17, 2005 1:03 pm
Posts: 9
Location: Round Rock, TX
Thank you Dennis Kozora for the suggestion. That worked perfectly!


Top
 Profile  
 
 Post subject: Re: NHibernate mapping
PostPosted: Fri Aug 31, 2007 9:51 am 
Newbie

Joined: Fri Dec 15, 2006 11:39 am
Posts: 7
I am using the latest version of NHibernate as well as MS SQL 2005, using C# code.

I have the same problem and was excited to find your solution.

Here is my mapping file:

<bag access="field" name="_inventoryTypes" cascade="all-delete-orphan" lazy="false" table="XrefProductInventoryTypes" schema="Costs">
<key column="CostIndex" />
<element column="InventoryTypeIndex" type="MyCompany.DomainModel.Costs.InventoryType, MyCompany" />
</bag>

The entire mapping file works perfectly except this part. All of the fields save except the bag is not being saved. I tried numerous combinations of different cascades, lazy, and access. Nothing worked. The file is compiled correctly and does throw an error. I even got so bold in my tries to change the name of the table to jiberish ('123' a table that doesn't exist). This didn't cause an error and everything else still worked. When I first started I was using this in a subclass. With it not working I pulled it completely out and put it in its own class with still no luck.

Is there something that I am doing wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 03, 2007 4:28 pm 
Beginner
Beginner

Joined: Wed Aug 29, 2007 1:25 pm
Posts: 26
Location: Recife, PE, Brazil
I think you don't need to declare sth like "companyRoles" in order to hold enumerations values.

But I think you will need to show your Enumetarion to NHibernate
it's a little different


There is a way to map enumeration that...you can find in this blog

http://sdesmedt.wordpress.com/?s=NHibernate

good luck!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 04, 2007 9:39 am 
Newbie

Joined: Fri Dec 15, 2006 11:39 am
Posts: 7
I read through several of the posts on the blog and was unable to locate anything that showed me how to show my enumerations to nhibernate. Can you clarify a little more please?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.