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.  [ 4 posts ] 
Author Message
 Post subject: subclass instance mismatch in table-per-subclass??
PostPosted: Mon Jan 19, 2009 7:58 pm 
Newbie

Joined: Fri Feb 08, 2008 2:41 pm
Posts: 18
It seems that Hibernate is unable to load two objects with the same base class (using the table per subclass strategy) at once. I have the following structure:

Code:
@Entity
@Table(name = "person")
@Inheritance(strategy = InheritanceType.JOINED)
public class Person implements Serializable {

    private String id;
    private String firstname;
    private String lastname;
    private char gender;
    private String address;
    private String phone;
// other properties plus getters and setters
}
@Entity
@Table(name = "customer")
@PrimaryKeyJoinColumn(name="customer_id")
public class Customer extends Person {

    private String workPlace;
    private String workPhone;
// other properties plus getters and setters
}

@Entity
@Table(name="employee")
@PrimaryKeyJoinColumn(name="employee_id")
public class Employee extends Person {
    private String position;
    private String username;
    private String password;
// other properties plus getters and setters
}

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Table(name = "invoice")
public class Invoice implements Serializable {

    private InvoiceID id;
    private Customer customer;
    private Employee salesperson;
    private Date date;
// other properties plus getters and setters
}


Everything works okay until we encounter an invoice where a customer is also an employee. So you have data in the employee and customer table all linked with the same id to the parent table.
The following is the error message that appeares when loading the invoice:
Code:
19:31:40,157 DEBUG SQL:111 - /* load hakim.model.Invoice */ select invoice0_.invoice_id ...... left outer join declaration declaratio5_ on invoice0_.invoice_id=declaratio5_.invoice_id and invoice0_.shop_id=declaratio5_.shop_id where invoice0_.invoice_id=? and invoice0_.shop_id=?
19:31:40,262  INFO DefaultLoadEventListener:134 - Error performing load command
org.hibernate.WrongClassException: Object with id: 1975121701 was not of the specified subclass: hakim.model.Employee (loaded object was of wrong class class hakim.model.Customer)
        at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1267)
        at org.hibernate.loader.Loader.getRow(Loader.java:1219)
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
        at org.hibernate.loader.Loader.doQuery(Loader.java:724)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
        at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
        at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
        at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
        at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
        at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
        at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
        at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
        at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
        at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
        at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
        at org.hibernate.impl.SessionImpl.get(SessionImpl.java:842)
        at org.hibernate.impl.SessionImpl.get(SessionImpl.java:835)
        at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:528)
        at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
.....
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
        at java.lang.Thread.run(Thread.java:619)
19:31:40,299 ERROR InvoiceController:70 - Error while finding invoices: org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: Object with id: 1975121701 was not of the specified subclass: hakim.model.Employee (loaded object was of wrong class class hakim.model.Customer); nested exception is org.hibernate.WrongClassException: Object with id: 1975121701 was not of the specified subclass: hakim.model.Employee (loaded object was of wrong class class hakim.model.Customer)

It seams that Hibernate doesn't know how to handle a situation where a customer and employee object refers to the same person. Everything works perfectly when they are different.
Could someone please advice me how to solve this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 21, 2009 7:45 pm 
Newbie

Joined: Fri Feb 08, 2008 2:41 pm
Posts: 18
I've been searching and it seems to me that this is a bug.
Is there a more experienced person who is willing to offer some help?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2009 5:20 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
It is not a bug, it is because your inheritance model: The same person-object cannot be an employee and a customer at the same time (In java inheritance is exclusive!). You didn't add your data to your database using Hibernate, did you?

I think you have to redesign your class model (for example instead of extending Person, add an association to person).

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re:
PostPosted: Wed May 20, 2009 11:27 am 
Newbie

Joined: Fri Feb 08, 2008 2:41 pm
Posts: 18
mmerder wrote:
I think you have to redesign your class model (for example instead of extending Person, add an association to person).

This is exactly how I solved it. thanks


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