Thursday, February 02, 2006

[Hibernate] Easy mapping example

As I 'm finally getting to know Hibernate basics, I managed to make all the mappings for my database. Here is an example for User

Some explanation, the database table is tbl_user, the persistence class is UserValue and the mapping is made in User.hbm.xml, Here are the example files:

SQL code create table tbl_User for postgreSQL:
create table "public"."tbl_permission" (
    permissionid serial,
    applicationid serial,
    roleid serial,
    autorizations text,
    begindate date,
    enddate date,
    constraint "pk_permission" primary key (permissionid),
    constraint "permissionsonapplication" foreign key (applicationid)
       references "tbl_application" (applicationid),
    constraint "rolehaspermissions" foreign key (roleid)
       references "tbl_role" (roleid)
);

The java persistence class UserValue:
package hibernate.databaseTables;
import java.sql.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMapping;

public class UserValue {
    private Integer userid=0;
    private String username="";
    private String password="";
    private String surname="";
    private String firstname="";
    private boolean sex=false;
    private String address="";
    private Integer zipcode=0;
    private String city="";
    private String email="";
    private String phone="";
    private String mobile="";
    private Date dateofbirth=new Date(0);
    public String getaddress() {
       return address;
    }
    public void setaddress(String address) {
       this.address = address;
    }
    public String getcity() {
       return city;
    }
    public void setcity(String city) {
       this.city = city;
    }
    public Date getdateofbirth() {
       return dateofbirth;
    }
    public void setdateofbirth(Date dateofbirth) {
       this.dateofbirth = dateofbirth;
    }
    public String getemail() {
       return email;
    public void setemail(String email) {
       this.email = email;
    }
    public String getfirstname() {
       return firstname;
    }
    public void setfirstname(String firstname) {
       this.firstname = firstname;
    public String getmobile() {
       return mobile;
    }
    public void setmobile(String mobile) {
       this.mobile = mobile;
    }
    public String getpassword() {
       return password;
    }
    public void setpassword(String password) {
       this.password = password;
    }
    public String getphone() {
       return phone;
    }
    public void setphone(String phone) {
       this.phone = phone;
    }
    public boolean getsex() {
       return sex;
    }
    public void setsex(boolean sex) {
       this.sex = sex;
    }
    public String getsurname() {
       return surname;
    }
    public void setsurname(String surname) {
       this.surname = surname;
    }
    public Integer getuserid() {
       return userid;
    }
    public void setuserid(Integer userid) {
       this.userid = userid;
    }
    public String getusername() {
       return username;
    }
    public void setusername(String username) {
       this.username = username;
    }
    public Integer getzipcode() {
       return zipcode;
    }
    public void setzipcode(Integer zipcode) {
       this.zipcode = zipcode;
    }
    public void reset(ActionMapping mapping, HttpServletRequest request) {
       username=null;
       password=null;
       surname=null;
       firstname=null;
       sex=false;
       address=null;
       zipcode=null;
       city=null;
       email=null;
       phone=null;
       mobile=null;
       dateofbirth=null;
    }
}

The mapping file UserRole.hbm.xml:
<?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>
    <class name="hibernate.databaseTables.UserRoleValue"
       table="tbl_userrole">
    <id name="userroleid" column="userroleid"
      type="java.lang.Integer">
       <!-- postgresql -->
       <generator class="sequence">
          <param name="sequence">tbl_userrole_userroleid_seq</param>
       </generator>
    </id>

    <property name="userid" column="userid" type="java.lang.Integer" />
    <property name="roleid" column="roleid" type="java.lang.Integer" />
    <property name="begindate" column="begindate" type="java.sql.Date" />
    <property name="enddate" column="enddate" type="java.sql.Date" />

    </class>
</hibernate-mapping>

No comments: