Search code examples
javasql-serverhibernatehibernate-mappingpojo

Mapping and POJO class creation for a database view in hibernate


Hai all I am using hibernate and Microsoft SQL server.

I have a view in my database as follows

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

For this view I had created a Pojo class like this

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

and Mapping file like this

<?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>

But when I am validating the Mapping file an error occurs like this

XML validation started.

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.

Can anyone help me to solve out this issue.

Thanks in advance.


Solution

  • Your mapping absolutely needs an id. I guess PID is your id so you should write something like this :

    <?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>