-->
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.  [ 5 posts ] 
Author Message
 Post subject: Mapping a Generic Dictionary
PostPosted: Sun Apr 22, 2007 1:25 am 
Newbie

Joined: Thu Mar 09, 2006 12:31 am
Posts: 8
Hello-

I have a field in my Company class called _authorizedUsers that is of type Dictionary<User, AuthorizationLevel>. User is a class that can already successively persist itself via nHibernate. AuthorizationLevel is an enum with two values: Read and Edit. I'm trying to save the dictionary to this table:

company_user
CompanyId INT (PK)
UserId INT (PK)
AuthorizationLevelId INT

Here's my bit from my mapping file that tries to handle this:

<!-- map the authorized users -->
<map name="_authorizedUsers" table="company_user" access="field" cascade="all">
<key column="CompanyId" />
<index-many-to-many column="UserId" class="User" />
<element column="AuthorizationLevelId" type="TestProject.AuthorizationLevel, TestProject" not-null="true" />
</map>

When I try to persist a Comapny object, I get this error:

NHibernate.MappingException: Invalid mapping information specified for type TestProject.Company, check your mapping file for property type mismatches

The mapping of collections is still fairly opaque to me. Can someone point me in the direction of where the error in my code may be? Thanks.

-Eric


Top
 Profile  
 
 Post subject: Trying a Different Approach
PostPosted: Sun Apr 22, 2007 4:00 am 
Newbie

Joined: Thu Mar 09, 2006 12:31 am
Posts: 8
So, after poking around a bit, I decided going to Dictionary route was likely going to be too painful (although I'd love someone to prove me wrong, since it is the optimal data structure). Instead, I created a class to associate the Company and the User. Here's what the table looks like:

Company_User
CompanyId INT (PK)
UserId INT (PK)
AuthorizationLevelId INT

And here's my mapping file for the CompanyUser class:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="TestProject"
assembly="TestProject">
<class name="CompanyUser" table="Company_User" lazy="false">
<composite-id>
<key-property column="CompanyId" name="Campaign" />
<key-property column="UserId" name="User" />
</composite-id>
<property name="AuthorizationLevel" column="AuthorizationLevelId" />
</class>
</hibernate-mapping>

Lastly, here's the bit of the mapping file in my Company mapping file that wires this all up:

<bag name="_authorizedUsers" access="field">
<key column="CampaignId" />
<one-to-many class="CampaignUser" />
</bag>

I've correctly set up the CompanyUser.hbm.xml file as an embedded resource as well. When I test the code, I get this error:

NHibernate.MappingException: Association references unmapped class: CampaignUser

But this class is mapped. Any ideas?

-Eric


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 2:32 pm 
Newbie

Joined: Mon Apr 02, 2007 12:31 pm
Posts: 19
I have a question about this line:
<one-to-many class="CampaignUser" />
from the Company mapping.

What's the CampaignUser class? Did you mean CompanyUser? If you changed the name from CompanyUser to CampaignUser, carefully check the CompanyUser.hbm.xml mapping file to make sure you've updated all the references in it. You might consider renaming the file too.

Sorry I can't comment on the use of a Dictionary.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 2:46 pm 
Newbie

Joined: Thu Mar 09, 2006 12:31 am
Posts: 8
Yeah, where it says "Campaign", it shoudl say "Company". My code has it correctly, it was just a cut-and-paste error in the snippet.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 3:35 pm 
Newbie

Joined: Mon Apr 02, 2007 12:31 pm
Posts: 19
So then is the error you're getting actually this?
NHibernate.MappingException: Association references unmapped class: CompanyUser

instead of the CampaignUser class being unmapped?

If the exception was correct and there is an association referencing CampaignUser, find that association and it may fix the problem.

How are you doing the factory configuration? Are you specifying mapping files, or are you loading the .dll and letting NHibernate find all the embedded resources?


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