0

i have a legacy database (which is still used by another legacy application) where the group is denormalized and duplicated into the child rows

table parent
(
  id
)

table child
(
  id
  parent_id
  group_id
  group_name
  group_Flag
  group_type
  name
)

and i would like to map them to

class Parent
{
    public long Id { get; private set; }
    public ICollection<Group> Groups { get; private set; }
}
class Group
{
    public long Id { get; set; }
    public string Name { get; set; }
    public GroupType Type { get; set; }
    public bool Flag { get; set; }
    public ICollection<Child> Childs { get; private set; }
}
class Child
{
    public long Id { get; private set; }
    public string Name { get; set; }
}
  1. Is this possible?
  2. How to do that in any of NHibernate's mapping methods (xml, MbC, Fluent, ...)

Update: Some additional info

  • the schema can't be changed because of the legacy application
  • additional views in the database are an option
  • leaking in the class model is possible
Firo
  • 30,626
  • 4
  • 55
  • 94

1 Answers1

0

Here are some starters :

You could try to have a look at the "mapping collections" section of NHibernate reference : - http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-collections

Then, you could try to map the Groups property of Parent class using the "where" clause of the collection mapping :

(9) where (optional) specify an arbitrary SQL WHERE condition to be used when retrieving or removing the collection (useful if the collection should contain only a subset of the available data)

And map the Childs property of the Group class the same way.

Otherwise, you could create some views to present your data differently, and map your objects to those views (setting update="false" and insert="false" to your identifier property mapping)

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
mathieu
  • 30,974
  • 4
  • 64
  • 90
  • what should i put into the where for the groups? group_xxx columns are the same for each child, the child_id's are unknown/different for each parent. – Firo Jul 16 '12 at 13:32
  • if i have my view `Groups ( group_id, parent_id )` how do i add new groups to this? – Firo Jul 16 '12 at 14:23
  • please state all your requirements in your question, otherwise it will be difficult to propose a correct solution to your problem :) – mathieu Jul 16 '12 at 15:16
  • thx for your time. The requirements are in the question, it is mapping 2 tables to 3 entities with 1-n associations between them. My comments where just to clarify your answer. – Firo Jul 17 '12 at 05:41