So I played a bit with a test project and I finally decided to add a column
to every sub-table of *A* of corresponding subclass which implement* IM*.
It's a bit annoying but I couldn't find a better way to make it work.
My mapping :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="ComponentNhibernateTest.Model"
namespace="ComponentNhibernateTest.Model"
>
<class name="A" table="a" >
<cache usage="read-write"/>
<id name="Id" type="Int64">
<generator class="native" />
</id>
<property name="Name" />
<joined-subclass name="A1" table="a1" >
<key column="Id" />
<property name="Foo" />
</joined-subclass>
<joined-subclass name="A2" table="a2" >
<key column="Id" />
<property name="Bar" />
<many-to-one name="C_Prop" class="C" column="cid" unique="true"
cascade ="all"/>
</joined-subclass>
</class>
<class name="B" table="b" >
<cache usage="read-write"/>
<id name="Id" type="Int64">
<generator class="native" />
</id>
<property name="Battr" />
<many-to-one name="C_Prop" class="C" column="cid" unique="true" cascade
="all"/>
</class>
<class name="C" table="c" >
<cache usage="read-write"/>
<id name="Id" type="Int64">
<generator class="native" />
</id>
<property name="Detail" />
<property name="OtherDetail" />
</class>
</hibernate-mapping>
My unittest:
A1 a1 = new A1
{
Name = "objectA1",
Foo = "foo"
};
A2 a2 = new A2
{
Name = "objectA2",
Bar = "Bar",
C_Prop = new C()
{
Detail = "detail a2",
OtherDetail = "other detail a2"
}
};
B b = new B
{
Battr="battre",
C_Prop = new C()
{
Detail = "detail b",
OtherDetail = "other detail b"
}
};
using (ISession session = NHibernateHelper.CreateSession())
using (ITransaction tx = session.BeginTransaction())
{
session.Save(a1);
session.Save(a2);
session.Save(b);
tx.Commit();
}
using (ISession session = NHibernateHelper.CreateSession())
{
a1 = session.Query<A1>().FirstOrDefault();
a2 = session.Query<A2>().FirstOrDefault();
b = session.Query<B>().FirstOrDefault();
}
Assert.IsNotNull(a1);
Assert.AreEqual(1, a1.Id);
Assert.IsNotNull(a2);
Assert.AreEqual(2, a2.Id);
Assert.IsNotNull(a2.C_Prop);
Assert.AreEqual(1, a2.C_Prop.Id);
Assert.IsNotNull(b);
Assert.AreEqual(1, b.Id);
Assert.IsNotNull(b.C_Prop);
Assert.AreEqual(2, b.C_Prop.Id);
Database structure:
CREATE TABLE c
(
id bigserial NOT NULL,
detail character varying,
otherdetail character varying,
CONSTRAINT c_pkey PRIMARY KEY (id)
)
WITH ( OIDS=FALSE );
ALTER TABLE c OWNER TO postgres;
CREATE TABLE a
(
id bigserial NOT NULL,
name character varying,
CONSTRAINT a_pkey PRIMARY KEY (id)
)
WITH ( OIDS=FALSE );
ALTER TABLE a OWNER TO postgres;
CREATE TABLE a1
(
id bigint NOT NULL,
foo character varying,
CONSTRAINT a1_pkey PRIMARY KEY (id),
CONSTRAINT a1_a_fkey FOREIGN KEY (id)
REFERENCES a (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH ( OIDS=FALSE );
ALTER TABLE a1 OWNER TO postgres;
CREATE TABLE a2
(
id bigint NOT NULL,
cid bigint,
bar character varying,
CONSTRAINT a2_pkey PRIMARY KEY (id),
CONSTRAINT a2_a_fkey FOREIGN KEY (id)
REFERENCES a (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT a2_c_fkey FOREIGN KEY (cid)
REFERENCES c (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH ( OIDS=FALSE );
ALTER TABLE a2 OWNER TO postgres;
CREATE TABLE b
(
id bigserial NOT NULL,
battr character varying,
cid bigint,
CONSTRAINT b_pkey PRIMARY KEY (id),
CONSTRAINT b_c_fkey FOREIGN KEY (cid)
REFERENCES c (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH ( OIDS=FALSE );
ALTER TABLE b OWNER TO postgres;
I'll be fine with this, but if someone find a solution of my first post, I
would be happy to know it.
Le lundi 14 mai 2018 17:41:13 UTC+2, [email protected] a écrit :
>
> Hi,
> I'm facing a problem that I can't make it work. I'm aware that it is a
> special case, but it could be very useful with my use of interface
>
> I have the following classes:
>
> - Class *A*
> - Class *A1* extends *A*
> - Class *A2* extends *A* implement *IM*
> - Class *B* implement *IM*
> - Class *C*
> - Interface *IM*
> - Property of Class *C C_prop*
>
> And the following tables:
>
> - Table *A*
> - column *C_id*
> - Table *A1*
> - Table *A2*
> - Table *B*
> - Column *C_id*
> - Table *C*
>
> For the Class *A2* I want to map the property of Class *C* to column
> *C_id* of *A*. So I want to map the child property C to the parent column
> of A.
> In the nhibernate documentation I found this in chapter 5.1.14 *"The
> <component> element maps properties of a child object to columns of the
> table of a parent class." *
> So it seems to do exactly what I want to achieve.
>
> So in mapping of Class* A* I have:
>
> <component name="*C_prop*" class="*A2*">
> <many-to-one name="*C_prop*" class="*C*" column="*C_id*"
> unique="true"/>
> </component>
>
> I have the following error:
> "Could not find a getter for property '*C_prop*' in class '*A*'"
>
> What do I do wrong?
> Thanks for your help.
>
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.