-->
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: Mapping single column from many-to-one to property
PostPosted: Sat Apr 12, 2008 11:33 am 
Newbie

Joined: Sun Mar 23, 2008 5:28 pm
Posts: 6
Hi,

I couldn't find an example of what I want to do.

I have an address class:

Code:
public class Address
{
        #region Fields
        ..
        #endregion

        public string Street..
        public string Region..
        public string City..
        public string Postcode..
        public string Country..   
}


Tables that use country map a countryid to the country table:

Code:
CREATE TABLE `Country`(
   `CountryId`      INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
   `Code`         VARCHAR(3),
   `Name`         VARCHAR(30),
   `Currency`      VARCHAR(3),
   CONSTRAINT `PK_Country` PRIMARY KEY CLUSTERED (`Countryid` ASC)
);


I would like to map just the country Name column from the country table to the country property on address class. I don't want to map to a country class and then gave to call getName. Is that possible. My current mapping is below.

Code:
<component name="Address" class="MyProject.Core.Global.Address, MyProject.Core">
        <property name="Street" column="Street" type="String"/>
        <property name="Region" column="Region" type="String"/>
        <property name="City" column="City" type="String"/>
        <many-to-one name="Country" column="Country" class="MyProject.Core.Global.Country, MyProject.Core"/>
        <property name="PostCode" column="PostCode" type="String"/>
</component>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 14, 2008 11:18 am 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
If you want classes that expose your data in a denormalized way, then create separate, non-entity classes for that. These "flattened" classes can then expose your data any way you want. You can even select instances of these classes from HQL queries using the new() syntax (see NHibernate documentation, chapter on HQL).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 14, 2008 1:54 pm 
Newbie

Joined: Sun Mar 23, 2008 5:28 pm
Posts: 6
Im sorry I don't follow. Isn't the Address class a "flattened" representation? Can you give and example?

Thanks for replying.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 14, 2008 2:58 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Assuming you have database tables and entities that are in 3rd normal form, you will have quite a number of child tables and "lookup" tables. For example, you may have a Person class that has a collection of Addresses, Phones, etc. The Address and Phone classes have references to "lookup" entities like State, Country, ZipCode, AddressType, PhoneType, etc.

For particular use cases, you will probably want to see a "flattened" view of the data -- e.g. a record that contains a person, their office address, their office phone, their mobile phone, their email address, etc. and also see descriptions (or other important values) from the "lookup" entities. To do this, you should make a class that takes all the entities it uses to produce the "flattened" view as constructor arguments:

Code:
public class PersonView
{
    private Person _person;
    private Address _workAddress;
    private Phone _officePhone;
    private Phone _moblePhone;
    private Email _officeEmail;
    ...

    public string FirstName
    {
        get { return (this._person != null ? this._person.FirstName: ""); }
        set { ... }
    }

    public string LastName
    {
        get { return (this._person != null ? this._person.LastName: ""); }
        set { ... }
    }

    public string WorkAddressLine1
    {
        get { return (this._workAddress != null ? this._workAddress.Line1: ""); }
    }

    public string WorkAddressCountryName
    {
        get { return this._workAddress != null && this._workAddress.Country != null ? this._workAddress.Country.Name : ""); }
    }
    ...

    public PersonView(Person person, Address officeAddress, Phone officePhone, Phone mobilePhone, Email officeEmail, ...)
    {
        this._person = person;
        this._workAddress = workAddress;
        ...
    }
}


Then in an HQL query, you can do this:

Code:
SELECT new PersonView(person, officeAddress, officePhone, mobilePhone, officeEmail ...)
FROM Person AS person
LEFT JOIN Person.Addresses AS workAddress
LEFT JOIN Person.Phones AS officePhone
LEFT JOIN Person.Phones AS mobilePhone
LEFT JOIN Person.Emails AS officeEmail
...
WHERE officeAddress.AddressType.Id = 'Office'
AND officePhone.PhoneType.Id = 'Office'
...


The point of all this is that your mapped entities should remain normalized, and any "denormalized views" that you need for particular use cases should be classes composed of your entities.


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.