Hibernate inheritence issue
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   suresh000
Posted On:   Thursday, April 4, 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 Code: @Entity @Table ( name = "person" ) @Inheritance(strategy=InheritanceType.JOINED) public class Person implements Serializable {    @Id    @Column ( name = "PERSON_ID" )    @GeneratedValue( strategy=GenerationType.AUTO)    private int                     personId;        @OneToMany ( mappedBy   More>>

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

Code:
@Entity
@Table ( name = "person" )
@Inheritance(strategy=InheritanceType.JOINED)
public class Person implements Serializable
{
   @Id
   @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

Code:
@Entity
@Table(name="patient")
@PrimaryKeyJoinColumn(name="person_id")
public class Patient extends Person implements Serializable
{
   @Column(name="patient_id")
   @GeneratedValue(strategy=GenerationType.AUTO)
   private int patientId;
}


My User bean is as follows
Code:
@Entity
@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

Code:
@Entity
@Table(name="address")
public class Address
{
   @Id
   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

Code:
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.

Thanks
Suresh T
   <<Less
About | Sitemap | Contact