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.  [ 12 posts ] 
Author Message
 Post subject: identifier of an instance of YYY altered from XXX to YYY
PostPosted: Wed Aug 30, 2006 8:57 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
NH Version: 1.2 Alpha

Hi!

When I try to load a Customer and after a Salesman (both inherits from Person) using NH, I get the following exception:

Quote:
identifier of an instance of Company.DTO.Vendedor altered from Bremen.DTO.Pessoa (Company.DTO.Pessoa) to Company.DTO.Vendedor (Bremen.DTO.Vendedor)


Ps: Customer = Cliente and Salesman = Vendedor :)

Anybody know what's this?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 12:25 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
I was reading that I can have only one single instance of persistant object representing same data inside a session ... but following the last sample (Salesman, Customer and Person), my Salesman can be a Customer and the Customer can be a Salesman (it's controlled by one filed into Person table).

So, how can I load the customer 10 (for example) and the salesman 10?

Do I need two sessions? :o

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 12:30 pm 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
mdsrlz wrote:
I was reading that I can have only one single instance of persistant object representing same data inside a session ... but following the last sample (Salesman, Customer and Person), my Salesman can be a Customer and the Customer can be a Salesman (it's controlled by one filed into Person table).

So, how can I load the customer 10 (for example) and the salesman 10?

Do I need two sessions? :o


Instead of using inheritance, maybe You should create association between person - salesman/customer. (One-to-one, if appropriate)

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 1:27 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
gert wrote:
mdsrlz wrote:
I was reading that I can have only one single instance of persistant object representing same data inside a session ... but following the last sample (Salesman, Customer and Person), my Salesman can be a Customer and the Customer can be a Salesman (it's controlled by one filed into Person table).

So, how can I load the customer 10 (for example) and the salesman 10?

Do I need two sessions? :o


Instead of using inheritance, maybe You should create association between person - salesman/customer. (One-to-one, if appropriate)

Gert


But is it true: I just can have ONE single instance of a persistant object inside a session?

And if I want to use inheritance, there is another way?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 2:16 pm 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
mdsrlz wrote:
But is it true: I just can have ONE single instance of a persistant object inside a session?

Think about it: If Hibernate has a Person with id=1 in memory, and another person with id=1, how could it possibly make the difference?

Maybe You can make so that at the mapiing level, the customer and Salesman are not extending the base class but entirely separate entities? Means, You map all Person properties twice, once inside Salesman and once inside Customer

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 9:06 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Ok gert.. I've understood.

But think with me: I have three tables: Customer, Salesman and Person. Customer and Salesman inherit (keep some common data) from Person, right?

How would you map this?

I think the most commom way is mapping as Customer, Salesman and Person class using the design "table-per-subclass", don't you agree with me?

So, don't you know any way to load one Customer and one Salesman that are the same person with tha same Session?

I'm asking this because my Customer Form has a FK to Salesman. When I load a customer I also need to load a DropDownList with all Salesmen. This is the time that two records (one Customer and one Salesman) will reference the same person.

Thanks again man!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 9:24 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
mdsrlz wrote:
I think the most commom way is mapping as Customer, Salesman and Person class using the design "table-per-subclass", don't you agree with me?


I would say that common way is to use person-role pattern: A person can have zero or more roles. Customer and Salesman are roles (inherited from PersonRole). A PersonRole belongs to exactly one Person.

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 10:16 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
gert wrote:
mdsrlz wrote:
I think the most commom way is mapping as Customer, Salesman and Person class using the design "table-per-subclass", don't you agree with me?


I would say that common way is to use person-role pattern: A person can have zero or more roles. Customer and Salesman are roles (inherited from PersonRole). A PersonRole belongs to exactly one Person.

Gert


My God, one more pattern? :o(
Could you give me a real example using my three tables?

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 10:43 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
mdsrlz wrote:
gert wrote:
I would say that common way is to use person-role pattern: A person can have zero or more roles. Customer and Salesman are roles (inherited from PersonRole). A PersonRole belongs to exactly one Person.


My God, one more pattern? :o(
Could you give me a real example using my three tables?


I would redesign those tables a little, if possible. If not possible, the PersoRole list can be calculated in code only, and might be removed.

Code:
class Person
{
  public virtual IList<PersonRole> Roles;
  // Following proeprties if database can not be altered
  // public virtual Customer CustomerRole;
  // public virtual Salesman SalesmanRole;
}

class PersonRole
{
  public virtual Person Person;
}

public Salesman : PersonRole
{
// salesman specific properties
}

public Customer : PersonRole
{
// Customer specific properties
}


Mapping for those should be rather trivial...
And the classes are quite obvious, so You did not ask for this example propably... But I have no better one also.

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 10:58 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Thanks gert.

But when I create a SalesMan there is one Person (1-to-1) assigned and when I create a Customer also there is one Person (1-to-1) assigned to this. In other words, when I load a Customer a Person is loaded and when I load a Salesman a Person is loaded too!

If the salesman and the customer recefece the same person, wouldn't it fail?

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 11:55 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
mdsrlz wrote:
But when I create a SalesMan there is one Person (1-to-1) assigned and when I create a Customer also there is one Person (1-to-1) assigned to this. In other words, when I load a Customer a Person is loaded and when I load a Salesman a Person is loaded too!

If the salesman and the customer recefece the same person, wouldn't it fail?


If SalesMan and Customer are loaded trought the same session object, they would reference the same person object. So, given that Person(id=1) has roles SalesMan(id=1) and Customer(id=1)
SalesMan s = session.GetObject<SalesMan>(1);
Customer c = session.GetObject<Customer>(1);
Person p = session.GetObject<Person>(1);

all of the follwing should be satisfied

object.ReferenceEquals(s.Person, p) == true
object.ReferenceEquals(c.Person, p) == true
object.ReferenceEquals(s.Person, c.Person) == true

Well, Lazy loading might create some mess... But I did not write it as c.Person == s.Person, as the Person might overload the Equals operator. And the "lazy loading mess" should not make difference from code standpoint.

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject: Interesting Design Problem
PostPosted: Mon Nov 06, 2006 3:09 pm 
Beginner
Beginner

Joined: Wed Aug 09, 2006 10:15 am
Posts: 20
Location: Vitoria - ES - Brazil
I have seen this design problem several times.
It has always been clear for me that a Employee, Employer, Customer, Salesman should not inherit from Person class.
You know why ? Think about the real world.
Nobody is borned as a Customer. Customer is a temporary state of a Person.
What if a Customer decides to work for this company ?
So a Customer can be an employee.
Have you ever though that there is a chance that a Person can be a Customer and an Employee at the same time ?
There is also another situation: A Person can be an Employee for a period of time and after some years he/she can work for the company again.
How can you tell when did he/she worked for the company ?

I would do something like this: ( There are other solutions but this is simple )

Code:
class Person
{
   IList lstEmployees; // List of Employess
   IList lstCustomers; // List of Customers
}

class Employee
{
   NullableDateTime initDate;  // when he/she started working for the company
   NullableDateTime endDate; // when he/she ended working
   JobPosition jobPosition; // Job Position ( Secretary, programmer .... )
   // Other information about the Employee
}

class Customer
{
    IList lstOrders; // Orders - Buyings realized by the Customer
    bool specialCustomer; // Is special Customer ???
   // Other information about the Customer
}


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