映射基础
(1)基本实体映射技术
类和表映射
<hibernate-mapping package="hibernate.test.domain">
<class name="Student" table="student" lazy="true">
class>
hibernate-mapping>
主键映射
普通字段映射
(2)多个字段作为主键
第一步:建立实体(Book)
多个字段作为主键时,需要重新建一个类,包含作为主键的各个字段。在这里命名为BookName。
//多个字段做主键
//Composite-id class must implement Serializable
public class BookName implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public BookName() {
super();
// TODO Auto-generated constructor stub
}
public BookName(String name, String author) {
super();
this.name = name;
this.author = author;
}
}
Book类:
package hibernate.test.domain;
public class Book {
private BookName id;
private String shortShow;
private String picture;
private String content;
public void setId(BookName id) {
this.id = id;
}
public BookName getId() {
return id;
}
public String getShortShow() {
return shortShow;
}
public void setShortShow(String shortShow) {
this.shortShow = shortShow;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
第二步: 映射文件(Book.hbm.xml)
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
第三步:配置文件
在mapping下添加映射文件位置:
<mapping resource="hibernate/test/xml/Book.hbm.xml">mapping>
第四部:测试 插入数据
public static void main(String[] args) {
Session session=null;
Transaction tx=null;
try {
session=HibernateUtil.getSession();
tx=session.beginTransaction();
BookName bookName=new BookName("快乐是本","ytt");
Book book=new Book();
book.setId(bookName);
book.setPicture("http://");
book.setShortShow("简单介绍");
book.setContent("介绍内容");
session.save(book);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
if(tx!=null)
tx.rollback();
}
finally{
if(session!=null)
session.close();
}
}
数据库验证,插入成功!
(3)组合关系映射(会产生很多冗余数据,大多还是通过一对多,多对多进行设计)
第一步 类的建立
public class Address {
private String city;
private String street;
private String zipcode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public Address(String city, String street, String zipcode) {
super();
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
public Address() {
super();
// TODO Auto-generated constructor stub
}
//组合关系测试
public class Person {
private int id;
private String name;
private Address address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
第二步 映射文件
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
第三步 配置文件
第四步 测试
public static void main(String[] args) {
// TODO Auto-generated method stub
Session session=null;
Transaction tx=null;
try {
session=HibernateUtil.getSession();
tx=session.beginTransaction();
Address address=new Address("郑州", "商都路G107国道", "0371");
Person person=new Person();
person.setName("ytt");
person.setAddress(address);
session.save(person);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
if(tx!=null)
{
tx.rollback();
}
}
finally{
session.close();
}
}
实际上是一张表,插入语句时这个:
Hibernate: insert into Person (name, city, street, zipcode) values (?, ?, ?, ?)
(4)大对象映射
Clob 文本大对象 最长4G
Blob 二进制数据大对象 最长4G
这个也不是很常用,就像其他类型一样使用,在映射文件中,type改为“java.sql.Blob/Clob”就行。