单向
一对一
实体:
@Entity
@Table(name="tb_product")
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne
@JoinColumn(name="dir_id")
private ProductDir dirId;
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the dirId
*/
public ProductDir getDirId() {
return dirId;
}
/**
* @param dirId the dirId to set
*/
public void setDirId(ProductDir dirId) {
this.dirId = dirId;
}
/**
* @Title: toString
* @author zjh
* @Desc:
* @return
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", dirId=" + dirId
+ "]";
}
}
@Entity
@Table(name="tb_productDir")
public class ProductDir {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @Title: toString
* @author zjh
* @Desc:
* @return
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "ProductDir [id=" + id + ", name=" + name + "]";
}
}
测试
public class TestDemo {
IProductDao iProductDao = new ProductDaoImpl();
IProductDirDao iProductDirDao = new ProductDirDaoImpl();
@org.junit.Test
public void Test(){
ProductDir productDir = new ProductDir();
productDir.setName("食品");
Product product = new Product();
product.setName("牛肉");
product.setDirId(productDir);
iProductDirDao.save(productDir);
iProductDao.save(product);
}
}
控制台打印
Hibernate: insert into tb_productDir (name) values (?)
Hibernate: insert into tb_product (dir_id, name) values (?, ?)
多对一
一的实体
一对多
一的实体
@Entity
public class Teacher {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany
//这里如果是要一对多关系模式的话,必须加上JoinColumn()注解,否则会生成一张关联表,那样的话就会被误解为多对多
@JoinColumn(name="student_id")
private List<Student> students = new ArrayList<>();
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the students
*/
public List<Student> getStudents() {
return students;
}
/**
* @param students the students to set
*/
public void setStudents(List<Student> students) {
this.students = students;
}
/**
* @Title: toString
* @author zjh
* @Desc:
* @return
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + "]";
}
}
多的实体
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @Title: toString
* @author zjh
* @Desc:
* @return
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
测试代码
public class OneToManyTest {
@Test
public void testAdd(){
Student student = new Student();
student.setName("tom");
Student student2 = new Student();
student2.setName("jreey");
Teacher teacher = new Teacher();
teacher.setName("jack");
teacher.getStudents().add(student);
teacher.getStudents().add(student2);
EntityManagerFactory entityManagerFactory = JPAUtils.getEntityManagerFactory();
EntityManager createEntityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = createEntityManager.getTransaction();
transaction.begin();
createEntityManager.persist(teacher);
createEntityManager.persist(student);
createEntityManager.persist(student2);
transaction.commit();
createEntityManager.close();
}
}
日志
Hibernate: insert into Teacher (name) values (?)
Hibernate: insert into Student (name) values (?)
Hibernate: insert into Student (name) values (?)
Hibernate: update Student set student_id=? where id=?
Hibernate: update Student set student_id=? where id=?