-->
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.  [ 9 posts ] 
Author Message
 Post subject: How to take advantage of register in general table?
PostPosted: Thu Oct 26, 2006 3:59 pm 
Newbie

Joined: Thu Oct 26, 2006 2:30 pm
Posts: 4
Hibernate version: 1.2.0
Hi,

I'm a newbie in NHibernate and I'm trying to use it to implement the following situation.
I have a general class called Person and two of it's specializations, Client and Manufacturer. I'm using the direct strategy to mapping this class inheritance, that is the table-per-subclass hierarchy.
Well, this mapping uses a relationship 1 to 1, which means that when I'm inserting a Manufacturer in the database I'm inserting, at same time, a register into the both tables, Person and Manufacturer. Well this behavior of the framework, at the same time that simplifies all the process, becomes my problem.
I'm saying this because when I try to insert an instance of Client to it's table, the framework is 'duplicating' the register of Person (the only field not equals to the previous person is the primary key, which is correct in the point of view of the OR mapping).
My question here then is, there is any way to take advantage of the register that was previously inserted in the table Person when I have inserted the instance of Manufacturer? If so, could you show me this way? If not, how can I solve this problem taking advantage of the person register to represent both the Manufacturer and the Client?


Top
 Profile  
 
 Post subject: I need an answer, please.
PostPosted: Fri Oct 27, 2006 1:00 pm 
Newbie

Joined: Thu Oct 26, 2006 2:30 pm
Posts: 4
Somebody can answer for me, please.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 1:52 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
Please post the table model in question, and what you are trying to do.
I don't understand what register is, and what do you mean by duplicating?


Top
 Profile  
 
 Post subject: Detailed description of the scenario
PostPosted: Mon Oct 30, 2006 8:23 am 
Newbie

Joined: Thu Oct 26, 2006 2:30 pm
Posts: 4
Ok, my mistake. What did I called register was the row value of the table. Taxonomy misplacing.

Well, the problem looks like this:

I have one table Person:

Code:
CREATE TABLE Person
(
  PERSONID    INT         NOT NULL,
  NAME        VARCHAR(40) NOT NULL
)


and two other tables:

Code:
CREATE TABLE Client
(
   PERSONID   INT    NOT NULL,
   GROUPID    INT    NOT NULL
)

CREATE TABLE Manufacturer
(
   PERSONID        INT   NOT NULL,
   NUM_PRODS_MAN   INT
)


Now, to implement this scenario using NHibernate, the best way is using table-per-subclass mapping, right? OK. Well, the table per subclass strategie uses a 1 to 1 relationship, which means that when I'm inserting an object Manufacturer I'm inserting in the database, at same time, a row into the both tables, Person and Manufacturer.

Ok, now considering this, let's try to insert an object Client into the database, the framework reacts in the same way, inserting another row in the Person table and a new row in the Client table, that' right?

Well, the problem is that my Client and Manufacturer tables are just a complement for the table Person and the Person can be, in my relational model, one Client AND one Manufacturer, which means that this row inserted while inserting the Client is undesirable.

What can I do to solve this, I do not want to insert this second row in the table Person and, instead of it, the framework NHibernate MUST UPDATE the data of the previous row inserted into the table Person.

Please, help me and thanks a lot.


Top
 Profile  
 
 Post subject: Re: Detailed description of the scenario
PostPosted: Mon Oct 30, 2006 4:55 pm 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
andre.luiz wrote:
What can I do to solve this, I do not want to insert this second row in the table Person and, instead of it, the framework NHibernate MUST UPDATE the data of the previous row inserted into the table Person.


You should use one-to-one relation instead of inheritance between person and Client classes

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 1:06 pm 
Newbie

Joined: Thu Oct 26, 2006 2:30 pm
Posts: 4
I used this approach and it did NOT work... when I defined the generator class as "assigned", the framework ALMOST achieved my intention, because it has inserted correctly into the table Client, but it has inserted too a row into the table Person...

I DO NOT want this insertion into the table Person... I already have a Person into this table that represents the Client that I'm inserting. How can I evict this insertion?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 4:25 pm 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
It is hard to tell what was wrong without seeing code and mappings.

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 8:57 am 
Beginner
Beginner

Joined: Fri Oct 20, 2006 8:02 am
Posts: 36
It seems you have:

Code:
public class Person {...}
public class Client : Person {...}
public class Manufacturer : Person {...}


I would map this scenario this way:

Code:
<class name="Person" table="Person">
    ...
    <joined-subclass name="Cliente" table="Cliente">
        ...
    </joined-subclass>
    <joined-subclass name="Manufacturer" table="Manufacturer">
        ...
    </joined-subclass>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 10:31 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
anuarneto wrote:
It seems you have:

Code:
public class Person {...}
public class Client : Person {...}
public class Manufacturer : Person {...}


I would map this scenario this way:

Code:
<class name="Person" table="Person">
    ...
    <joined-subclass name="Cliente" table="Cliente">
        ...
    </joined-subclass>
    <joined-subclass name="Manufacturer" table="Manufacturer">
        ...
    </joined-subclass>
</class>


He wants that one Person can be Cliente and Manufacturer at same time. So he can not use the sub-classes. Or the class inheritance in .Net code.

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.