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.  [ 1 post ] 
Author Message
 Post subject: Inheritance - Table per subclass problem
PostPosted: Mon Oct 02, 2006 1:09 pm 
Beginner
Beginner

Joined: Wed Jul 26, 2006 11:52 am
Posts: 23
Location: Edinburgh, Scotland
First of all i've this is a problem i'm having with NHibernate, sorry for posting it here as well as in the NHibernate forum but I really need a solution to it....if I shouldn't be posting to both forums let me know and I wont do it again.

Does anyone outt here have any experiance of implementing inheritance using the Table Per Subclass approach? If so i'd really appreciate some help with the following problem. I'm sure its something simple....but I can't get it to work and i've been at it for ages!

I have the following model (Sorry I can't draw it, don't know how to insert drawings here)

Company has many Communications.
Comunications is an abstract class, from which the class Meetings inherits.

The problem i'm having is that when I create a new Meeting for an existing Company my meetings table is populated with the foreign key to the communications table however the communications table has a null value in its FK column to the company table. Any help would be greatly appreciated....

Mapping File
========
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="sampleApp.Communication, sampleApp" table="rme_communications">
<id name="Id" column="comm_id" unsaved-value="" type="int">
<generator class="assigned" />
</id>
<property name="Title" column="comm_title"/>
<property name="Date" column="comm_date" type="date"/>
<joined-subclass name="sampleApp.Meeting, sampleApp" table="rme_meetings">
<key column="mtn_com_id"></key>
<property
name="Location" column="mtn_location"/>
<property
name="Type" column="mtn_type"/>
<property
name="Rating" column="mtn_rtng"/>
</joined-subclass>
<joined-subclass name="sampleApp.Report, sampleApp" table="rme_reports">
<key column="rpt_com_id"></key>
<property
name="Type" column="rpt_type"/>
</joined-subclass>
</class>
<class name="sampleApp.Instrument, sampleApp" table="rme_instruments">
<id name="Code" column="inst_code" unsaved-value="" type="String" length="100">
<generator class="assigned" />
</id>
<property name="Sedol" column="inst_sedol"/>
<property name="Description" column="inst_desc"/>
<property name="DateFirstHeld" column="inst_first_held"/>
<property name="DateLastHeld" column="inst_last_held" />
</class>
<class name="sampleApp.Company, sampleApp" table="rme_companies">
<id name="Id" column="coy_code" unsaved-value="" type="int" >
<generator class="assigned" />
</id>
<property name="Name" column="coy_name"/>
<property name="Address" column="coy_address"/>
<bag name="Instruments">
<key column="inst_coy_code" />
<one-to-many
class="sampleApp.Instrument, sampleApp" />
</bag>
<bag name="Communications" lazy="true" cascade="save-update" inverse="true">
<key column="comm_coy_code" />
<one-to-many
class="sampleApp.Communication, sampleApp" />
</bag>
<!--<map name="instruments">
<key column="inst_coy_code"/>
<index column="inst_code" type="String" length ="100"/>
<one-to-many class="sampleApp.Instrument, sampleApp" />
</map>-->
</class>
</hibernate-mapping>

Company Class
===========
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace sampleApp
{
public class Company
{
private int _id;

public virtual int Id
{
get { return _id; }
set { _id = value; }
}

private string _name;

public virtual string Name
{
get { return _name; }
set { _name = value; }
}

private string _address;

public virtual string Address
{
get { return _address; }
set { _address = value; }
}

private IList _instruments;

public virtual IList Instruments
{
get { return _instruments; }
set { _instruments = value; }
}

private IList _communications;

public virtual IList Communications
{
get { return _communications; }
set { _communications = value; }
}
public Company()
{
Instruments = new ArrayList();
Communications = new ArrayList();
}

public Company(IList p_communications, IList p_instruments)
{
Instruments = p_instruments;
Communications = p_communications;
}

public Company(int p_id, string p_name, string p_address, IList p_instruments)
{
Id = p_id;
Name = p_name;
Address = p_address;
Instruments = p_instruments;
}

}
}

Communication Class
==============
using System;
using System.Collections.Generic;
using System.Text;

namespace sampleApp
{
public abstract class Communication
{
private int _id;

public virtual int Id
{
get { return _id; }
set { _id = value; }
}

private string _title;

public virtual string Title
{
get { return _title; }
set { _title = value; }
}

private DateTime _date;

public virtual DateTime Date
{
get { return _date; }
set { _date = value; }
}

}
}

Meetings Class
==========
using System;
using System.Collections.Generic;
using System.Text;

namespace sampleApp
{
public class Meeting : Communication
{
private string _location;

public virtual string Location
{
get { return _location; }
set { _location = value; }
}

private string _type;

public virtual string Type
{
get { return _type; }
set { _type = value; }
}

private string _rating;

public virtual string Rating
{
get { return _rating; }
set { _rating = value; }
}

public Meeting()
{
}
public Meeting(int p_id, string p_title, DateTime p_date, String p_location)
{
Id = p_id;
Title = p_title;
Date = p_date;
Location = p_location;
}
}
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.