Hi,
I'm working with
Hibernate version: 1.2.0.4000 on .NET 2.0 using MS-SQL-Server 2005.
Implementing a polymorphic tree I got a strange problem with nodes, where the child collection should return mixed subtypes.
The base class is
Code:
public class ProdStructNodeWithChilds
{
[...]
private IList<ProdStructNodeWithChilds> childs;
public virtual IList<ProdStructNodeWithChilds> Childs
{
get { return childs; }
protected set { childs = value; }
}
[...]
}
There are three derived classes doing nothing special in sense of persistence
Code:
public class Profile : ProdStructNodeWithChilds
{ [...] }
public class ProductGroup : ProdStructNodeWithChilds
{ [...] }
public class Product : ProdStructNodeWithChilds
{ [...] }
This classes are mapped following the one-table-per-hierarchy pattern. The entity table looks like
Code:
CREATE TABLE cc3_OrgStructNode (
ID int IDENTITY(1,1) NOT NULL,
Type char(4) NOT NULL, // Subclassing descriminator
Name nvarchar(50) NOT NULL,
[...]
// Some subclass fields
[...]
CONSTRAINT PK_OrgStructNode PRIMARY KEY CLUSTERED ( ID ASC ) "
)
For the relations in the the tree is an additional table
Code:
CREATE TABLE dbo.cc3_OSNParent_2_OSNChild(
ParentID int NOT NULL,
ChildID int NOT NULL,
SortOrder int NOT NULL,
CONSTRAINT PK_cc3_OSNParent_2_OSNChild PRIMARY KEY CLUSTERED (ParentID ASC, ChildID ASC)
)
The mapping is :
Code:
<class name="ProdStructNodeWithChilds" discriminator-value="PSNC">
<list
name="Childs"
table="cc3_OSNParent_2_OSNChild"
generic="true">
<key column="ParentID"/>
<index column="SortOrder" />
<many-to-many column="ChildID" class="ProdStructNodeWithChilds"/>
</list>
<subclass name="Profile" discriminator-value="Prfl" />
<subclass name="ProductGroup" discriminator-value="PrGr" />
<subclass name="Product" discriminator-value="Prod" />
</class>
All works fine so far, when the relation table only returns elemens of one subtype.
F.E. if a "Profile" node only has "Product"s as childs, the Childs collection will contain the "Product"s as expected. When a "Profile" only has "ProductGroup"s as child, the Childs collection will contain the "ProductGroups"s as expected.
BUT the problem: If there is a mixed set of subtypes in the db relation, f.e. "Product"s and "ProductGroup"s as children of a "Profile", the "Childs" collection only has elements of one of the subtypes after loading the "Profile". In my case such mixed db relation on return "ProductGroup" elements (may be random result).
There is no exception, the log shows nothing special.
Any ideas? How can I accomplish such mixed type child relations?
Thanks in advance for any information.
(Beg your pardon for my poor englisch.)
Regards
Martin