今天改了一整天终于搞出来了,用的打注解的方式,用部门和员工间的关联关系做例子(打注解实在是比写xml方便多了....)。
一个部门有多个员工。
代码如下
1.创建部门和员工的实体类,并打上注解
(1)员工类
package dayentity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="tb_emp")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name")
private String name;
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;
}
}
(2)部门类
package dayentity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="tb_dept")
public class Dept {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int dno;
private String name;
@OneToMany
@Column(name="id")
private Set<Employee> employee=new HashSet<>();
public int getDno() {
return dno;
}
public void setDno(int dno) {
this.dno = dno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Employee> getEmployee() {
return employee;
}
public void setEmployee(Set<Employee> employee) {
this.employee = employee;
}
}
(3)记得把类文件加载到hibernate.cfg.xml文件中,然后写测试类
package dayentity;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class test0001 {
public static void main(String[] args) {
Configuration cfg=new Configuration().configure();
SessionFactory sessionFactory=cfg.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction ts=session.beginTransaction();
Dept dept=new Dept();
dept.setDno(111);
dept.setName("锤锤");
Employee emp=new Employee();
emp.setId(11);
emp.setName("lsj");
dept.getEmployee().add(emp);
session.save(dept);
session.save(emp);
ts.commit();
session.close();
}
}
最后应该有三张表
....放不了图..就不看了
补充一点 级联操作 cascade 应该写在set配置里
有四种选择 all 所有操作都进行级联操作
save_update 只有在保存和更新时才进行级联操作
delete 只有在删除时进行级联操作
none 所有操作都不进行级联操作
补充一下 用写配置文件的方式做一对多映射
(1)写实体类
部门类
package day31;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
public class Dept {
private int dno;
private String name;
private Set<Employee> employee=new HashSet<>();
public int getDno() {
return dno;
}
public void setDno(int dno) {
this.dno = dno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Employee> getEmployee() {
return employee;
}
public void setEmployee(Set<Employee> employee) {
this.employee = employee;
}
}
员工类
package day31;
public class Employee {
private int id;
private String name;
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;
}
}
(2)配置文件
部门类
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="day31">
<class name="Dept" table="dept">
<id name="dno" column="dno" type="integer">
<generator class="native"/>
</id>
<property name="name"/>
<set name="employee" table="employee">
<key column="dno"></key>
<one-to-many class="day31.Employee"/>
</set>
</class>
</hibernate-mapping>
员工类
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="day31">
<class name="Employee" table="employee">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
(3)
测试类
package day31;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class test0001 {
public static void main(String[] args) {
Configuration cfg=new Configuration().configure();
SessionFactory sessionFactory=cfg.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction ts=session.beginTransaction();
Dept dept=new Dept();
dept.setDno(112);
dept.setName("song");
Employee emp=new Employee();
emp.setId(12);
emp.setName("dudu");
dept.getEmployee().add(emp);
session.save(dept);
session.save(emp);
ts.commit();
session.close();
}
}