在hibernate中为数据库视图创建映射和POJO类

时间:2014-09-17 10:36:47

标签: java sql-server hibernate hibernate-mapping pojo

Hai所有我使用的是hibernate和Microsoft SQL服务器。

我的数据库中有一个视图如下

create view [dbo].[Pat_Det] As  Select patientid As pid,title, fname, mname,lname,dob,gender,mstatus,idtype,idno,mtongue,emailid,smsstatus,mailstatus,status,regcenter,regdate from dbo.LAB_patientreg WHERE 1=1

对于这个视图,我创建了一个像这样的Pojo类

package POJO;

import java.io.Serializable;
import java.util.Date;

public class AddressViewPojo implements Serializable{

    String pid, title, fname, mname, lname, sex, mstatus, idtype, aadhar_no, emailid, regcenter;
    Date dob, date_created;
    int allow_sms, allow_email, status;

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getMname() {
        return mname;
    }

    public void setMname(String mname) {
        this.mname = mname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getMstatus() {
        return mstatus;
    }

    public void setMstatus(String mstatus) {
        this.mstatus = mstatus;
    }

    public String getIdtype() {
        return idtype;
    }

    public void setIdtype(String idtype) {
        this.idtype = idtype;
    }

    public String getAadhar_no() {
        return aadhar_no;
    }

    public void setAadhar_no(String aadhar_no) {
        this.aadhar_no = aadhar_no;
    }

    public String getEmailid() {
        return emailid;
    }

    public void setEmailid(String emailid) {
        this.emailid = emailid;
    }

    public String getRegcenter() {
        return regcenter;
    }

    public void setRegcenter(String regcenter) {
        this.regcenter = regcenter;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public Date getDate_created() {
        return date_created;
    }

    public void setDate_created(Date date_created) {
        this.date_created = date_created;
    }

    public int getAllow_sms() {
        return allow_sms;
    }

    public void setAllow_sms(int allow_sms) {
        this.allow_sms = allow_sms;
    }

    public int getAllow_email() {
        return allow_email;
    }

    public void setAllow_email(int allow_email) {
        this.allow_email = allow_email;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }
}

和像这样的映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-lazy="false">
  <class name="POJO.AddressViewPojo"  table="Pat_Det">
      <property name="aadhar_no" column="aadhar_no"></property>
      <property name="allow_email" column="allow_email"></property>
      <property name="allow_sms" column="allow_sms"></property>
      <property name="date_created" column="date_created"></property> 
      <property name="dob" column="dob"></property>
      <property name="emailid" column="emailid"></property>
      <property name="fname" column="emailid"></property>
      <property name="idtype" column="idtype"></property>
      <property name="lname" column="lname"></property>
      <property name="mname" column="mname"></property>
      <property name="mstatus" column="mstatus"></property>
      <property name="pid" column="pid"></property>
      <property name="regcenter" column="regcenter"></property>
      <property name="sex" column="sex"></property>
      <property name="status" column="status"></property>
      <property name="title" column="title"></property>          
  </class>
</hibernate-mapping>

但是当我验证Mapping文件时,会发生这样的错误

开始进行XML验证。

Checking file:/E:/akshai/TREVALAB/src/MAPPING/AddressViewPojo.hbm.xml...

The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)". [21] 

XML validation finished.

任何人都可以帮我解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:2)

你的映射绝对需要一个id。我猜PID是你的id所以你应该写这样的东西:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-lazy="false">
  <class name="POJO.AddressViewPojo"  table="Pat_Det">
      <id name="pid" column="pid"></id> <!-- Right here -->
      <property name="aadhar_no" column="aadhar_no"></property>
      <property name="allow_email" column="allow_email"></property>
      <property name="allow_sms" column="allow_sms"></property>
      <property name="date_created" column="date_created"></property> 
      <property name="dob" column="dob"></property>
      <property name="emailid" column="emailid"></property>
      <property name="fname" column="emailid"></property>
      <property name="idtype" column="idtype"></property>
      <property name="lname" column="lname"></property>
      <property name="mname" column="mname"></property>
      <property name="mstatus" column="mstatus"></property>
      <property name="regcenter" column="regcenter"></property>
      <property name="sex" column="sex"></property>
      <property name="status" column="status"></property>
      <property name="title" column="title"></property>          
  </class>
</hibernate-mapping>