0

I have a query in mapping.

I have a class named Patient which is mapped One to One with another class named User. I need to link user name form User class with patient class.

Patient Class

@Entity 
@Table(name="PATIENTDETAILS")
public class Patient {

@Column (nullable=false)
private String firstname;
@Column (nullable=false)
private String lastname;
private String nickname;
@Column (nullable=false)
private String birthdate;
@Column (nullable=false)
private String streetaddress;
@Column (nullable=false)
private String city;
@Column (nullable=false)
private String state;
@Column (nullable=false)
private String zipcode;
@Column (nullable=false)
private String gender;
@Column (nullable=false)
private String maritalstatus;
@Id
@Column (nullable=false)
private String ssn;
@Column (nullable=false)
private String email;
private int homephone;
private int workphone;
private int cellphone;
@Column (nullable=false)
private String preferredphone;
@Column (nullable=false)
private String race;
@Column (nullable=false)
private String ethnicity;
@Column (nullable=false)
private String preferredlanguage;
@Column (name="name_relationship1")
private String relation1;
@Column (name="phonenumber")
private int phone1;
private String initial1;
@Column (name="name_relationship2")
private String relation2;
@Column (name="phone_number")
private int phone2;
private String initial2;
@Column (nullable=false,name="User_Name")
private String userName;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "username")
private User user;

User Class:

@Entity 
@Table(name="USERSTABLE")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int userid;
@Column (nullable=false,name="username")
private String username;
@Column (nullable=false)
private String password;
private String gender;
@Column (nullable=false)
private String role;
@Column (nullable=false)
private String email;
private int age;
private String secretquestion;
private String answer;

By running the above query, User name from User table is getting added as foreign key in Patient Table.

The user will be created by sign up process and once he submits the sign up form.

enter image description here

In my case, if a new patient signs up, his credentials(User name and Password) will be created by above process.

After that he will be redirected to a patient form (Only if the patient is new) where he needs to fill his details and once he submits the form, it will be updated in the patient table. enter image description here

While updating the patient table, the username which was derived from User table is not getting updated.

I am trying to initialize User class and set the user name as below:

        patient.setPreferredphone(preferredphone);
        patient.setRace(race);
        patient.setEthnicity(ethnicity);
        patient.setPreferredlanguage(preferredlanguage);
        patient.setRelation1(relation1);
        patient.setPhone1(phone1);
        patient.setInitial1(initial1);
        patient.setRelation2(relation2);
        patient.setPhone2(phone2);
        patient.setInitial2(initial2);


        patient.getUser().setUsername(uname); (Setting the user name)

I have also tried various other combinations, in spite of that I am not able to set the user name in patient table.

Am I going wrong anywhere? We will not be able to update the table unless both user table and Patient table are getting updated simultaneously? Since in my case, user table is getting updated first and patient table is updated next when ever new patient signs up?. Could you please help me with this?.

When I give extends I am getting an error like below.

ERROR: org.springframework.web.context.ContextLoader - Context        
initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean     
with name 'userDao' defined in ServletContext resource [/WEB-  
INF/spring/root-context.xml]: Instantiation of bean failed; nested exception  
is java.lang.ExceptionInInitializerError

Caused by: java.lang.ClassCastException:    
org.hibernate.mapping.SingleTableSubclass cannot be cast to   
org.hibernate.mapping.RootClass
at 
org.hibernate.cfg.annotations.PropertyBinder.bind(PropertyBinder.java:225)
SRS
  • 439
  • 9
  • 19
  • Can you just make Patient extend User? – riddle_me_this Apr 16 '15 at 17:53
  • When I give extends I am getting error as stated in my description. Is there any other way to implement this? – SRS Apr 16 '15 at 23:06
  • Remove the @Id annotation in the subclass (and next time, check SO: http://stackoverflow.com/questions/12087011/spring-3-1-hibernate-4-exception-for-inheritance-cannot-be-cast-to-org-hibernat) – riddle_me_this Apr 17 '15 at 00:18
  • Just to confirm, I need to remove the annotations and need to make subclass extend super class.. Am i right?.. – SRS Apr 17 '15 at 01:27
  • Just the @Id annotation in your subclass. There's no need for it since you're defining it in the parent class and it's giving you the exception. Extending is one way to do it. – riddle_me_this Apr 17 '15 at 23:10

0 Answers0