Hibernate inheritence issue
0 posts in topic
Thread View Thread View

Hibernate inheritence issue...
Thu Apr 04, 2013 12:19 AM

Hi All,
I am using Hibernate 4.2.0 with spring framework integration.

I am using the hibernate inheritance for mapping Person to Patient and User.

My bean definition is as follows

@Table ( name = "person" )
public class Person implements Serializable
   @Column ( name = "PERSON_ID" )
   @GeneratedValue( strategy=GenerationType.AUTO)
   private int                     personId;
   @OneToMany ( mappedBy="person", fetch=FetchType.EAGER)
   private Set<Address> addresses;

   @Column ( name = "FIRST_NAME" )
   private String                  firstName;

   @Column ( name = "LAST_NAME" )
   private String                  lastName;

My Patient bean is defined as follows

public class Patient extends Person implements Serializable
   private int patientId;

My User bean is as follows
@Table(name = "user")
@PrimaryKeyJoinColumn(name = "person_id")
public class User extends Person implements Serializable
   @Column(name = "USER_ID")
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private int userId;
   @Column(name = "INSTITUTION_NAME")
   private String institutionName;

As Address is common for both Patient and User, I put the mapping in the Person bean.

My Address bean definition is as follows

public class Address
   private int aid;
   private String street;
   private String city;
   private String state;
   private String country;

The problem is when I want to retrieve all the user based on city, I am getting the following error.

org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass:User (loaded object was of wrong class class Patient)

The issue is when fetching the data hibernate check for the sub classes for Person table and wrongly take the Patient class instead of the User class. I create the criteria with User.class entity only,

I am using the criteria to query hibernate . Following is the code for retrieve the data

List <User> list = sessionFactory.openSession ().createCriteria ( User.class )
            .createAlias( "addresses","addr")
            .add(Restrictions.ilike("addr.city", "%" + streetName+"%"))            
            .list ();

Kindly anyone suggest that I am missing anything or any work around to solve this issues.

Suresh T
About | Sitemap | Contact