-->
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.  [ 3 posts ] 
Author Message
 Post subject: Inverted 'Component' mapping?
PostPosted: Wed Jun 20, 2007 1:29 pm 
Newbie

Joined: Wed Jun 20, 2007 9:51 am
Posts: 2
Hi
I'm just another beginner in need of help ;)
Reading NHibernate's documentation I've found "Component mapping" feature which maps child properties to parent's row. I need something inverted - I'd like to have data from child row mapped to the parent object. How to do this?
Here is detailed description of my problem:
I have many-to-many asociation between Products and Customers (there are some limitations in product 'visibility' for some customers, prices are different too). So, in fact, I have 3 classes:


Code:
class Product
{
  int Id
  string Name
  string PartNumber
}

class Customer
{
  int Id
  string Name
  string Code
}

class P2CLink
{
  int Id
  Product Product
  Customer Customer
  decimal Price
  string CustomersPartNumber
}

All of these objects are stored in tables: Products, Customers, P2CLinks.
Mapping schema of the first two is obvious, P2CLink has following mapping:
Code:
  <class name="P2CLink" table="P2CLink">
    <id name="Id">
      <column name="Id" sql-type="int" not-null="true"/>
      <generator class="identity" />
    </id>
    <many-to-one class="Product" name="Product" fetch="join" column="ProdId"/>
    <many-to-one class="Customer" name="Customer" fetch="join" column="CustId"/>
    <property name="Price" formula="GetPrice(ProdId,CustId)" type="string"/>
  </class>


What I need is to display Pricelist for given Customer with ability to make some filtration or search by the Part Number or Customer's Part Number.
I've made a 'view' which inherits P2CLink as following
Code:
class NiceP2CLinkView:P2CLink
{
  string Name {return Product.Name;}
  string PartNumber {return product.PartNumber;}
}


So, the pricelist now looks ok, but... performing a query is a little bit frustrating - I can't query for "view" properties, so I have to build some nasty nested criteria for querying for Product or for Customer.
I think, there has to be some simpler and cleaner solution, however I have no idea how to get it in the other way.

============
Edit: Ops, I forgot It's NHibernate 1.2 and .NET 2.0 (C#)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 2:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
This may have less to do with Component mapping than querying. It sounds to me this is what you want to do:

Code:
IQuery q = session.CreateQuery("from P2CLink link where link.Product.Name = :productName");
q.SetParameter("productName", "Kit Kat");
IList list = q.List();

There is of course the equivalent code using the Criteria interface, I am just more familiar with HQL.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 3:23 pm 
Newbie

Joined: Wed Jun 20, 2007 9:51 am
Posts: 2
hmmm... I have to disagree, however your query will work perfectly and will return proper value. Problem is not in the query but in it's structure or - more precisely - in what are you asking about.
If all interested properties are available from parent, why bother about querying children?
There are 2 main disadvantages:
1. Using that kind of query forces you to build your own user interface to create or edit filter/search functionality. Flattening object gives you ability to use ready made solutions without building the "where" section by hand.
2. Much more important thing is maintainability of code and code reuse. Imagine that in more or less distant future someone will have to enhance that functionality. He (or she) will be confused, why it has been done that way instead performing simple query. I know - it should be in the documentation, but being honest - who reads it ;)


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