首先上两个实体类:
package entity;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Date;
/**
* Created by raid on 2016/5/14.
*/
@Entity
public class Students {
private Integer sid;
private String sname;
private String gender;
private Date birthday;
private String address;
private Address ad;
public Students() {
}
public Students(Integer sid, String sname, String gender, Date birthday, String address) {
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
public Students(String sname, String gender, Date birthday, String address) {
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
@Id
@Column(name = "sid")
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
@Basic
@Column(name = "sname")
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
@Basic
@Column(name = "gender")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Basic
@Column(name = "birthday")
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Basic
@Column(name = "address")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Basic
@Column(name = "ad")
public Address getAd() {
return ad;
}
public void setAd(Address ad) {
this.ad = ad;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Students students = (Students) o;
if (sid != null ? !sid.equals(students.sid) : students.sid != null) return false;
if (sname != null ? !sname.equals(students.sname) : students.sname != null) return false;
if (gender != null ? !gender.equals(students.gender) : students.gender != null) return false;
if (birthday != null ? !birthday.equals(students.birthday) : students.birthday != null) return false;
if (address != null ? !address.equals(students.address) : students.address != null) return false;
return true;
}
@Override
public int hashCode() {
int result = sid != null ? sid.hashCode() : 0;
result = 31 * result + (sname != null ? sname.hashCode() : 0);
result = 31 * result + (gender != null ? gender.hashCode() : 0);
result = 31 * result + (birthday != null ? birthday.hashCode() : 0);
result = 31 * result + (address != null ? address.hashCode() : 0);
return result;
}
}
package entity;
/**
* Created by raid on 2016/5/15.
*/
public class Address {
private String postcode;
private String phone;
private String ad;
public Address() {
}
public Address(String postcode, String phone, String ad) {
this.postcode = postcode;
this.phone = phone;
this.ad = ad;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAd() {
return ad;
}
public void setAd(String ad) {
this.ad = ad;
}
}
上面两个类,其实就一个实体Students,Address是包含在Students中的属性,所以对于的Students.hbm.xml文件应该这样配置:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Students" table="students" schema="hibernate">
<id name="sid" column="sid">
<generator class="native"/>
</id>
<property name="sname" column="sname"/>
<property name="gender" column="gender"/>
<property name="birthday" column="birthday"/>
<property name="address" column="address"/>
<component name="ad" class="entity.Address">
<property name="postcode" column="postcode"/>
<property name="phone" column="phone"/>
<property name="ad" column="ad"/>
</component>
</class>
</hibernate-mapping>
其中,主键为自增长类型,所以需要配置:
<generator class="native"/>
如果数据库存储的类型是datetime类型,那么实体类就要设置成java.util.Date这个类
如果属性是另外一个类的话,那么就需要使用component标签来实现。
在配置文件中,name是实体类的数据域名称,column是对应的数据库的属性名称。