-->
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 normalized data to flags...
PostPosted: Thu Jun 07, 2007 10:41 am 
Newbie

Joined: Thu May 10, 2007 10:54 am
Posts: 3
Location: central illinois, usa
NHibernate version: 1.2 GA
Name and version of the database you are using: mssql2005

I can not think of any other way than to describe this database but as being normalized, but I can not for the life of me figure out how to map the following structure to my object.

Regarding the code below, it has been simplified immensely and I have typed most of it out by hand, I simply want to focus on the mapping of the BankCertified and Incorporated bools from the structure indicated below.

Any help is greatly appreciated.

Thanks,
Dave

Tables:
Code:
CREATE TABLE dbo.Agents(
   Region char(2) NOT NULL,
   State char(2) NOT NULL,
   Code char(4) NOT NULL,
   FirstName varchar(35) NOT NULL,
   LastName varchar(40) NOT NULL,
   PRIMARY KEY CLUSTERED (Region, State, Code)
)
GO
CREATE TABLE dbo.BankCertifiedAgents(
   Region char(2) NOT NULL,
   State char(2) NOT NULL,
   Code char(4) NOT NULL,
   PRIMARY KEY CLUSTERED (Region, State, Code)
)
GO
CREATE TABLE dbo.IncorporatedAgents(
   Region char(2) NOT NULL,
   State char(2) NOT NULL,
   Code char(4) NOT NULL,
   PRIMARY KEY CLUSTERED (Region, State, Code)
)
GO
ALTER TABLE dbo.BankCertifiedAgents ADD FOREIGN KEY(Region, State, Code) REFERENCES dbo.Agents (Region, State, Code)
GO
ALTER TABLE dbo.IncorporatedAgents ADD FOREIGN KEY(Region, State, Code) REFERENCES dbo.Agents (Region, State, Code)
GO


Classes:
Code:
public class Agent {
   private Name _name;
   private bool _bankCertified;
   private bool _incorporated;

   public virtual Name { get { return _name; } protected set { _name = value; } }
   public virtual bool BankCertified { get { return _bankCertified; } protected set { _bankCertified = value; } }
   public virtual bool Incorporated { get { return _incorporated; } protected set { _incorporated = value; } }
}

public class Id {
   private string _region;
   private string _state;
   private string _code;

   public virtual string Region { get { return _region; } protected set { _region = value; } }
   public virtual string State { get { return _state; } protected set { _state = value; } }
   public virtual string Code { get { return _code; } protected set { _code = value; } }
}

public class Name {
   private string _first;
   private string _last;

   public virtual string First { get { return _first; } protected set { _first = value; } }
   public virtual string Last { get { return _last; } protected set { _last = value } }
}


Mapping Document:
Code:
<class name="Agent" table="Agents" schema="dbo" mutable="false" lazy="true">
   <composite-id class="Id">
      <key-property name="Region"/>
      <key-property name="State"/>
      <key-property name="Code"/>
   </composite-id>
   <component name="Name" class="Name">
      <property name="First" column="FirstName"/>
      <property name="Last" column="LastName"/>
   </component>
   <!--- help -- BankCertified -->
   <!--- help -- Incorporated -->
</class>


Top
 Profile  
 
 Post subject: Mostly just curious...
PostPosted: Thu Jun 07, 2007 12:35 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
It depends on how much you "simiplified" away in relating this issue. I have to believe that there is actually data in those tables that is simply unneeded for your application. Otherwise... I'm straining at charity here.

Anyway, the option I see (that only kind of gives you what you want): map them as separate classes with a <one-to-one> association. This might seem more expensive, but it would seem less expensive to absorb the left join as part of the load, rather than testing each and every row with a separate query as a computed column. If you need bools, they could be properties that create or dereference said associated objects. Of course, I'm just spitballing here, so YMMV.

But it should work even if there aren't other columns in those tables. Even if there are and you just don't care, you don't have to map them. (Of course, if there are non-null columns you didn't report, you'll not be able to actually create instances of the associated objects without mapping them and providing at least dummy data.)

And if there really aren't other columns in those tables: please don't sully the word "normalized" so.


Top
 Profile  
 
 Post subject: Re: Mapping normalized data to flags...
PostPosted: Thu Jun 07, 2007 2:30 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
djseng wrote:
Tables:
Code:
CREATE TABLE dbo.Agents(
   Region char(2) NOT NULL,
   State char(2) NOT NULL,
   Code char(4) NOT NULL,
   FirstName varchar(35) NOT NULL,
   LastName varchar(40) NOT NULL,
   PRIMARY KEY CLUSTERED (Region, State, Code)
)
GO
CREATE TABLE dbo.BankCertifiedAgents(
   Region char(2) NOT NULL,
   State char(2) NOT NULL,
   Code char(4) NOT NULL,
   PRIMARY KEY CLUSTERED (Region, State, Code)
)
GO
CREATE TABLE dbo.IncorporatedAgents(
   Region char(2) NOT NULL,
   State char(2) NOT NULL,
   Code char(4) NOT NULL,
   PRIMARY KEY CLUSTERED (Region, State, Code)
)
GO
ALTER TABLE dbo.BankCertifiedAgents ADD FOREIGN KEY(Region, State, Code) REFERENCES dbo.Agents (Region, State, Code)
GO
ALTER TABLE dbo.IncorporatedAgents ADD FOREIGN KEY(Region, State, Code) REFERENCES dbo.Agents (Region, State, Code)
GO


Classes:
Code:
public class Agent {
   private Name _name;
   private bool _bankCertified;
   private bool _incorporated;

   public virtual Name { get { return _name; } protected set { _name = value; } }
   public virtual bool BankCertified { get { return _bankCertified; } protected set { _bankCertified = value; } }
   public virtual bool Incorporated { get { return _incorporated; } protected set { _incorporated = value; } }
}

public class Id {
   private string _region;
   private string _state;
   private string _code;

   public virtual string Region { get { return _region; } protected set { _region = value; } }
   public virtual string State { get { return _state; } protected set { _state = value; } }
   public virtual string Code { get { return _code; } protected set { _code = value; } }
}

public class Name {
   private string _first;
   private string _last;

   public virtual string First { get { return _first; } protected set { _first = value; } }
   public virtual string Last { get { return _last; } protected set { _last = value } }
}


Mapping Document:
Code:
<class name="Agent" table="Agents" schema="dbo" mutable="false" lazy="true">
   <composite-id class="Id">
      <key-property name="Region"/>
      <key-property name="State"/>
      <key-property name="Code"/>
   </composite-id>
   <component name="Name" class="Name">
      <property name="First" column="FirstName"/>
      <property name="Last" column="LastName"/>
   </component>
   <!--- help -- BankCertified -->
   <!--- help -- Incorporated -->
</class>


This is actually quite simple. All you want is to add a <joined-subclass> element in that mapping file for your BankCertifiedAgent and your IncorporatedAgent.

That is unless you mean that someone could be both a BankCertified agent and an Incorporated agent. If that is the case, then yes you probably need to use a One-to-One sort of mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 08, 2007 10:55 am 
Newbie

Joined: Thu May 10, 2007 10:54 am
Posts: 3
Location: central illinois, usa
OK, thanks for the input, after being scolded by marcal, I've made some modifications to my views, I was getting a little too granular with my data I believe.

Thanks again


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.