Department.java
package com.many2one.bean;
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.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="department",catalog="Hibernate_Many2One")
public class Department {
private int id;
private String name;
private Set<Employee> sets;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(mappedBy="department")
public Set<Employee> getSets() {
return sets;
}
public void setSets(Set<Employee> sets) {
this.sets = sets;
}
}
Employee.java
package com.many2one.bean;
import javax.persistence.CascadeType;
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.Table;
@Entity
@Table(name="employee",catalog="Hibernate_Many2One")
public class Employee {
private int id;
private String name;
private Department department;
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="deptid")
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
导出的sql
create table Hibernate_Many2One.department (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
create table Hibernate_Many2One.employee (
id integer not null auto_increment,
name varchar(255),
deptid integer,
primary key (id)
)
alter table Hibernate_Many2One.employee
add index FK4722E6AE3829E242 (deptid),
add constraint FK4722E6AE3829E242
foreign key (deptid)
references Hibernate_Many2One.department (id)
测试代码
@Test
public void insert(){
Session session=HibernateSessionFactory.getSession();
Transaction transaction=session.beginTransaction();
try {
transaction.begin();
Department department=new Department();
department.setName("软件测试部");
Employee employee=new Employee();
employee.setName("张三");
employee.setDepartment(department);
session.save(employee);
transaction.commit();
} catch (HibernateException e) {
e.printStackTrace();
}
}
@Test
public void select(){
Session session=HibernateSessionFactory.getSession();
try {
Employee employee=(Employee) session.get(Employee.class, 1);
System.out.println(employee.getName());
System.out.println(employee.getDepartment().getName());
} catch (HibernateException e) {
e.printStackTrace();
}
}