-->
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.  [ 2 posts ] 
Author Message
 Post subject: load a subclass with several field values.
PostPosted: Mon Mar 31, 2008 3:27 pm 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Hello,

I've a table called Product. In this tables there are two columns called ElementWeight_Unit and ElementWeight_Val.
On the business Side I have two classes that looks like this:

public class Product
{
public int Id {get; set;}
public string Name {get; set;}
public Quantity Weight {get; set;}
}


public class Quantity
{
public float Value {get; set;}
public string Unit {get; set;}
}

I would like to load the data of the two colums (ElementWeight_Unit and ElementWeight_Val) in the property "Weight" of type Quantity.

How can I achieve this ?

Regards,

mathmax


Top
 Profile  
 
 Post subject: load a subclass with several field values
PostPosted: Mon Mar 31, 2008 3:58 pm 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi mathmax,

Your best option is probably to make Quantity implement IUserType, then you can map it directly:
http://www.milkcarton.com/blog/2006/12/31/CompositeUserTypes+In+NHibernate.aspx

Alternatively, you may be able to use protected properties for the individual parts of the Weight if the Quantity class is immutable:
Code:
protected virtual float Value
{
    get { return _value; }
    set { _value = value; }
}

protected virtual string Unit
{
    get { return _unit; }
    set { _unit = value; }
}

protected virtual Quantity Weight // do not map this
{
    get { return new Quantity(_unit, _value); }
    set { _unit = value.Unit; _value = value.Value; }
}


Regards,
Richard


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