-->
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: Person DB (Father, Mother & Childrens)
PostPosted: Sat Jan 23, 2010 8:07 pm 
Newbie

Joined: Sat Jan 23, 2010 7:41 pm
Posts: 2
I need map simple table:
Code:
[ID]: integer
[FirstName]: TEXT
[LastName]: TEXT
[Father]: INTEGER
[Mother]: INTEGER


to class
Code:
class Person{
public string FirstName{get;set;}
public string LastName{get;set;}
public Person Father{get;set;}
public Person Mother{get;set;}
public IList<Person> Childrens{get;set;}


I write a workaround solution with 2 one-to-many:
Code:
class Person{
private int id;
private IList<Person> _childrens1 = new List<Person>();
private IList<Person> _childrens2 = new List<Person>();
public string FirstName{ get; set;}
public string LastName{ get; set;}
public byte Gender { get; set; }
public virtual Person Father{ get; set;}
public virtual Person Mother{ get; set;}
public virtual IList<Person> Childrens
{
   get
   {
      List<Person> ret= new List<Person>( _childrens1 );
      ret.AddRange( _childrens2 );
      return ret;
   }
   protected set { }
}

public virtual IList<Person> Childrens1
{
   get { return _childrens1; }
   protected set { _childrens1 = value; }
}

public virtual IList<Person> Childrens2
{
   get { return _childrens2; }
   protected set { _childrens2 = value; }
}
public virtual void AddChildren(Person child)
{
   if (Gender == Male)
   {
      if (child.Father != null)
      {
         child.Father._childrens1.Remove( child );
      }
      child.Father = this;
      _childrens1.Add(child);
   }
   if (Gender == Female)
   {
      if (child.Mother != null)
      {
         child.Mother._childrens2.Remove(child);
      }
      child.Mother = this;
      _childrens2.Add(child);
   }
   
}
const byte Male = (byte)'M';
const byte Female = (byte)'F';
}


and mapping:
Code:
<class name="Person" table="Person" lazy="false">
   <id name="id" unsaved-value="0" column="ID" type="System.Int32" access="field">
      <generator class="identity" />
   </id>

   <property name="FirstName" column="FirstName"/>
   <property name="LastName" column="LastName"/>
   
   <bag name="Childrens1" fetch="select" inverse="true">
      <key column="Father"/>
      <one-to-many class="Person" not-found="ignore"/>
   </bag>
   <many-to-one name="Father" fetch="select" column="Father" class="Person" not-found="ignore"/>

   <bag name="Childrens2" fetch="select" inverse="true">
      <key column="Mother"/>
      <one-to-many class="Person" not-found="ignore"/>
   </bag>
   <many-to-one name="Mother" fetch="select" column="Mother" class="Person" not-found="ignore"/>
</class>


But "it is not cool" for my tutor.
Does a not-workaround solution exist?


Last edited by falcon.mk2 on Sun Jan 24, 2010 4:58 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Person DB (Father, Mother & Childrens)
PostPosted: Sun Jan 24, 2010 3:42 am 
Newbie

Joined: Tue Jan 05, 2010 12:15 pm
Posts: 9
Hi,

you should first tell what you want to do and what you need to accomplish and then where you didn't find a solution for that you needed a workaround at all.
If i look at the only table you posted as well as the first class, the differences to the classes in the workaround are quite big.

If the title say it all, then i would propose one class.

Code:
public enum Gender
{
  Male,
  Female
}

public class Person
{
  string firstName;
  string lastName;
  Person mother;
  Person father;
  List<Person> children;
  Gender gender;
}

Is it that we are talking about?


Top
 Profile  
 
 Post subject: Re: Person DB (Father, Mother & Childrens)
PostPosted: Sun Jan 24, 2010 4:57 am 
Newbie

Joined: Sat Jan 23, 2010 7:41 pm
Posts: 2
Yes. But I don't know how to build a normal ORM, without adding two fields.

mother-childrens is a one-to-many & father-childrens is a one-to-many. I add two auxiliary properties (Childrens1 & Childrens2) to help build ORM. But does a normal (direct) mapping exits?


Top
 Profile  
 
 Post subject: Re: Person DB (Father, Mother & Childrens)
PostPosted: Sun Jan 24, 2010 7:09 am 
Newbie

Joined: Tue Jan 05, 2010 12:15 pm
Posts: 9
Did not found a solution than your workaround, but it looks like the current java version is able to do better, since they support formulas for the keys instead only columns.


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.