SQL
create table Department
(
id int not null auto_increment,
name varchar(20),
primary key (id)
);
create table Employee
(
id int not null auto_increment,
name varchar(20),
deptid int,
primary key (id)
);
alter table Employee add constraint FK_Reference_1 foreign key (deptid)
references Department (id) on delete restrict on update restrict;
Department.java
package com.many2one.bean;
import java.io.Serializable;
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;
@SuppressWarnings("serial")
@Entity
@Table(name="department",catalog="jpa_many2one")
public class Department implements Serializable{
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="jpa_many2one")
public class Employee {
private int id;
private String name;
private Department department;
@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;
}
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="deptid")
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="JPA_Many2OnePU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.many2one.bean.Department</class>
<class>com.many2one.bean.Employee</class>
<properties>
<property name = "hibernate.connection.driver_class" value = "com.mysql.jdbc.Driver"/>
<property name = "hibernate.connection.url" value = "jdbc:mysql://localhost:3306/jpa_many2one"/>
<property name = "hibernate.connection.username" value = "root"/>
<property name = "hibernate.connection.password" value = "root"/>
<property name = "hibernate.show_sql" value = "true"/>
<property name = "hibernate.format_sql" value = "true"/>
</properties>
</persistence-unit>
</persistence>
JPAUtil.java
package com.many2one.util;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAUtil {
private static EntityManager entityManager;
public static EntityManager getInstance(){
if(entityManager!=null){
return entityManager;
}else{
return makeInstance();
}
}
private static synchronized EntityManager makeInstance() {
if(entityManager==null){
EntityManagerFactory entityManagerFactory=Persistence.createEntityManagerFactory("JPA_Many2OnePU");
return entityManagerFactory.createEntityManager();
}
return null;
}
}
@Test
public void select(){
EntityManager entityManager=JPAUtil.getInstance();
Employee employee=entityManager.find(Employee.class, 1);
System.out.println("雇员是:"+employee.getName());
System.out.println("部门是:"+employee.getDepartment().getName());
}
@Test
public void insert(){
EntityManager entityManager=JPAUtil.getInstance();
EntityTransaction entityTransaction=entityManager.getTransaction();
try {
entityTransaction.begin();
Department department=new Department();
department.setName("软件研发部");
Employee employee=new Employee();
employee.setName("张三");
employee.setDepartment(department);
entityManager.persist(employee);
entityTransaction.commit();
} catch (Exception e) {
e.printStackTrace();
entityTransaction.rollback();
}
}