-->
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.  [ 4 posts ] 
Author Message
 Post subject: many-to-one mapping of simple values instead of objects
PostPosted: Tue Jan 01, 2008 2:49 pm 
Newbie

Joined: Tue Jan 01, 2008 2:21 pm
Posts: 4
Consider the following class:

class Item
{
public int ID;
public string Name;
public IList Categories; //this is a collection of strings--category names
}

and the database schema:

CREATE TABLE Item(ID int NOT NULL, Name varchar(50) NOT NULL, CONSTRAINT PK_Item PRIMARY KEY CLUSTERED (ID ASC))

CREATE TABLE Category(ID int NOT NULL, Name varchar(50) NOT NULL, CONSTRAINT PK_Category PRIMARY KEY CLUSTERED (ID ASC))

CREATE TABLE ItemCategory(ItemID int, CategoryID INT)

ALTER TABLE [dbo].[ItemCategory] WITH CHECK ADD CONSTRAINT [FK_ItemCategory_Category] FOREIGN KEY([CategoryID])
REFERENCES [dbo].[Category] ([ID])

ALTER TABLE [dbo].[ItemCategory] CHECK CONSTRAINT [FK_ItemCategory_Category]

ALTER TABLE [dbo].[ItemCategory] WITH CHECK ADD CONSTRAINT [FK_ItemCategory_Item] FOREIGN KEY([ItemID])
REFERENCES [dbo].[Item] ([ID])

ALTER TABLE [dbo].[ItemCategory] CHECK CONSTRAINT [FK_ItemCategory_Item]

In short, there are three tables: Item, Category, and the association table ItemCategory.

The Item class has a collection of category names (strings), but NOT category objects.

What is the correct mapping in this situation? I know how to create the mapping with objects, but not with plain strings. In other words, I don't want Categories to be treated as objects, but rather just strings.

<class name="MyAssembly.Item, MyAssembly" table="Item">
<id name="ID" column="ID" type="Int32">
<generator class="identity" />
</id>
<property name="Name" column="Name" type="String" length="50"/>
<bag name="Categories" table="ItemCategory">
<key column="ItemID"/>
<many-to-one column="CategoryID" ??? /> <!--this should retrieve Category.Name from table Category-->
</bag>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 02, 2008 1:53 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
See http://www.hibernate.org/hib_docs/nhibe ... s-ofvalues

Scroll down to the first example (line starting, "Some examples, first, a set of strings:...").


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 02, 2008 11:03 pm 
Newbie

Joined: Tue Jan 01, 2008 2:21 pm
Posts: 4
This does not accomplish what I am trying to do. The mapping:

<set name="Categories" table="ItemCategories">
<key column="ItemID"/>
<element column="CategoryName" type="String"/>
</set>

would work if CategoryName were stored in table ItemCategories. However, ItemCategories only has ItemID and CategoryID. CategoryName comes from table Category instead (please see my schema above).

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 12:50 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Fair enough! Didn't read your question closely enough. I don't know that what you want to do is possible with vanilla config. Maybe with a custom UserType, but I'm not experienced with that.


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