Hibernate中的映射问题

时间:2015-04-16 17:14:13

标签: mysql spring hibernate

我有一个映射查询。

我有一个名为Patient的类,它与另一个名为User的类映射为One。我需要将User name的用户名与患者类相关联。

患者等级

@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;

用户类:

@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;

通过运行上述查询,用户表中的用户名将作为外键添加到患者表中。

用户将通过注册过程创建,并在提交注册表单后生成。

enter image description here

就我而言,如果新病人报名,他的证书(用户名和密码)将由上述过程创建。

之后,他将被重定向到患者表格(只有患者是新的),他需要填写他的详细信息,一旦他提交表格,它将在患者表格中更新。 enter image description here

更新患者表时,从User表派生的用户名不会更新。

我正在尝试初始化User类并将用户名设置如下:

        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)

我还尝试了其他各种组合,尽管我无法在患者表中设置用户名。

我在哪里出错了?除非用户表和Patient表同时更新,否则我们将无法更新表格?因为在我的情况下,用户表首先得到更新,然后在新病人注册时更新患者表?你可以帮帮我吗?。

当我给予延伸时,我收到如下错误。

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)

0 个答案:

没有答案