Mapping
public void MapDomain(ObjectRelationalMapper orm)
{
orm.Patterns.PoidStrategies.Add(new IdentityPoidPattern());
orm.TablePerClass(domainClasses);
orm.OneToOne<Client, Company>();
orm.OneToOne<Client, Contact>();
}
Test
var sf = nhconf.BuildSessionFactory();
using(var s = sf.OpenSession())
using (var tx = s.BeginTransaction())
{
var client1 = new Client {Name = "Client1"};
client1.Company = new Company {Name = "Company1", Client = client1};
client1.Contact = new Contact { FirstName = "Contact1", Client = client1 };

var client2 = new Client {Name = "Client2"};
client2.Company = new Company { Name = "Company2", Client = client2};
client2.Contact = new Contact { FirstName = "Contact2", Client = client2 };

s.Persist(client1);
s.Persist(client2);
tx.Commit();
}
sf.Statistics.Clear();
using (var s = sf.OpenSession())
using (var tx = s.BeginTransaction())
{
var l = s.CreateQuery("from Client cl left join fetch cl.Company left join
fetch cl.Contact").List<Client>();
foreach (var client in l)
{
NHibernateUtil.IsInitialized(client.Company).Should().Be.True();
NHibernateUtil.IsInitialized(client.Contact).Should().Be.True();
string a = client.Company.Name;
string b = client.Contact.FirstName;
}
sf.Statistics.QueryExecutionCount.Should().Be(1);
tx.Commit();
}

does not fail.

On Fri, Jul 23, 2010 at 1:16 PM, Blaise <[email protected]> wrote:

> Hi,
>
> I have what I think should be a fairly simple scenario:
>
> I have Contacts, Companies and Clients.
> A Client can reference either a Contact or a Company.
>
> Here are the classes:
>
> public class Client {
> public virtual int Id { get; set; }
> public virtual string Code { get; set; }
> public virtual string Name { get; set; }
> public virtual int Status { get; set; }
> public virtual Company Company { get; set; }
> public virtual Contact Contact { get; set; }
> }
>
> public class Company {
> public virtual int Id { get; set; }
> public virtual string Name { get; set; }
> public virtual Client Client { get; set; }
> }
>
> public class Contact {
> public virtual int Id { get; set; }
> public virtual string FirstName { get; set; }
> public virtual string LastName { get; set; }
> public virtual Client Client { get; set; }
> }
>
>
> and the corresponding conf:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
> assembly="TestNH"
> namespace="TestNH.Domain">
>
>
> <class name="Client">
> <id name="Id" column="id">
> <generator class="identity"></generator>
> </id>
> <property name="Code" column="code" type="string"/>
> <property name="Name" column="name" type="string"/>
> <property name="Status" column="status" type="int"/>
> <many-to-one name="Company" class="Company" column="companyId"
> unique="true" />
> <many-to-one name="Contact" class="Contact" column="contactId"
> unique="true" />
> </class>
>
> <class name="Contact">
> <id name="Id" column="id">
> <generator class="identity"></generator>
> </id>
> <property name="FirstName" column="firstName" type="string"/>
> <property name="LastName" column="lastName" type="string"/>
> <one-to-one name="Client" class="Client" property-ref="Contact"/>
> </class>
>
> <class name="Company">
> <id name="Id" column="id">
> <generator class="identity"></generator>
> </id>
> <property name="Name" column="name" type="string"/>
> <one-to-one name="Client" class="Client" property-ref="Company"/>
> </class>
>
> </hibernate-mapping>
>
>
> The database schema is pretty straightforward.
> The link between Client and Contact/Company is implemented through two
> foreign keys in the table Client: companyId and contactId.
>
> Hi have two problems with this that I can't seem to solve.
>
> 1) The first one I can live with:
> A HQL query "from Contact" will load the Client with a bunch of
> SELECTs instead of doing a outer join, regardless of the value of the
> attribute "fetch" in the one-to-one relationship. The only way to do a
> join is if I explicitely do it in HQL like "from Contact c left join
> fetch c.Client"
>
> 2) The second one is more problematic:
> If I do the following query "from Client as c left join fetch
> c.Contact left join fetch c.Company", it will not only do a correct
> select on the Client table with two outer left joins on Contact and
> Company, but it will also generate one select per result on the Client
> table. Apparently it doesn't realise that it has already loaded the
> Client entity in the first select :
>
> NHibernate: select top 20 client0_.id as id0_0_, contact1_.id as
> id1_1_, company2_.id as id2_2_, client0_.code as code0_0_,
> client0_.name as name0_0_, client0_.status as status0_0_,
> client0_.companyId as companyId0_0_, client0_.contactId as
> contactId0_0_, contact1_.firstName as firstName1_1_,
> contact1_.lastName as lastName1_1_, company2_.name as name2_2_ from
> Client client0_ left outer join Contact contact1_ on
> client0_.contactId=contact1_.id left outer join Company company2_ on
> client0_.companyId=company2_.id
> NHibernate: SELECT client0_.id as id0_0_, client0_.code as code0_0_,
> client0_.name as name0_0_, client0_.status as status0_0_,
> client0_.companyId as companyId0_0_, client0_.contactId as
> contactId0_0_ FROM Client client0_ WHERE client0_.company...@p0;@p0 =
> 1696
> NHibernate: SELECT client0_.id as id0_0_, client0_.code as code0_0_,
> client0_.name as name0_0_, client0_.status as status0_0_,
> client0_.companyId as companyId0_0_, client0_.contactId as
> contactId0_0_ FROM Client client0_ WHERE client0_.contact...@p0;@p0 =
> 248
> etc....
>
>
> Thanks for any pointer that can help me with that.
>
> Blaise
>
>
> PS: this is a copy of
> https://forum.hibernate.org/viewtopic.php?f=25&t=1005962,
> but since apparently this is the new place to post, I'm duplicating
> it.
> Incidentally, another post on the old forum reports the same issue:
> https://forum.hibernate.org/viewtopic.php?f=25&t=1000516
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>


-- 
Fabio Maulo

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to