在学习数据的关联关系时候,请把容器映射看一看.不然不容易理解关联关系.
环境: Myeclispe+mysql.
(1) 创建表
--删除表
drop table if exists book ;
drop table if exists person;
--新建表
create table person
(
pid int not null auto_increment primary key ,
name varchar(20) not null ,
age int
);
create table book
(
id int not null auto_increment primary key,
book_name varchar(20) not null ,
publish varchar(20) ,
pid int ,
foreign key(pid) references person(pid) on delete cascade
);
--事务提交
commit ;
(2) Peson.java 类
package org.hibernate.onetomany.vo;
import java.util.HashSet;
import java.util.Set;
public class Person {
private int id ;
private String name ;
private int age ;
private Set books = new HashSet() ;
public Set getBooks() {
return books;
}
public void setBooks(Set books) {
this.books = books;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
(3) Person.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="org.hibernate.onetomany.vo.Person" table="person" >
<id name="id" type="java.lang.Integer">
<column name="pid"></column>
<generator class="increment"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name"></column>
</property>
<property name="age" type="java.lang.Integer">
<column name="age"></column>
</property>
<set name="books" table="book" cascade="all" inverse="true" >
<key >
<column name="pid"></column>
</key>
<one-to-many class="org.hibernate.onetomany.vo.Book" />
</set>
</class>
</hibernate-mapping>
(3) Book.java
package org.hibernate.onetomany.vo;
public class Book {
private int id ;
private String bookName ;
private String publish ;
private Person person ;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
}
(4)Book.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="org.hibernate.onetomany.vo.Book" table="book">
<id name="id" type="java.lang.Integer">
<column name="id"></column>
<generator class="increment">
</generator>
</id>
<property name="bookName" type="java.lang.String">
<column name="book_name"></column>
</property>
<property name="publish">
<column name="publish"></column>
</property>
<many-to-one name="person" class="org.hibernate.onetomany.vo.Person" insert="true" update="true">
<column name="pid" ></column>
</many-to-one>
</class>
</hibernate-mapping>
(5) PersonDao.java
package org.hibernate.onetomany.dao;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.onetomany.vo.Book;
import org.hibernate.onetomany.vo.Person;
public class PersonDao {
private Session session ;
public PersonDao()
{
session = new Configuration().configure().buildSessionFactory()
.openSession() ;
}
public void insert(Person p)
{
session.save(p) ;
session.beginTransaction().commit() ;
session.close() ;
}
public static void main(String[] args) {
PersonDao pd = new PersonDao() ;
Person p = new Person() ;
p.setName("zhangsan") ;
p.setAge(1) ;
Book b1 = new Book() ;
b1.setBookName("jspjsp") ;
b1.setPublish("2007-10.12") ;
b1.setPerson(p) ;
p.getBooks().add(b1) ;
pd.insert(p) ;
}
}