JPA映射关系

爱被打了一巴掌 2023-05-31 10:08 139阅读 0赞

JPA映射关系

  • 单向
    • 一对一
    • 多对一
    • 一对多
  • 双向
    • 一对多/多对一

单向

一对一

实体:

  1. @Entity
  2. @Table(name="tb_product")
  3. public class Product {
  4. @Id
  5. @GeneratedValue(strategy=GenerationType.IDENTITY)
  6. private Long id;
  7. private String name;
  8. @OneToOne
  9. @JoinColumn(name="dir_id")
  10. private ProductDir dirId;
  11. /**
  12. * @return the id
  13. */
  14. public Long getId() {
  15. return id;
  16. }
  17. /**
  18. * @param id the id to set
  19. */
  20. public void setId(Long id) {
  21. this.id = id;
  22. }
  23. /**
  24. * @return the name
  25. */
  26. public String getName() {
  27. return name;
  28. }
  29. /**
  30. * @param name the name to set
  31. */
  32. public void setName(String name) {
  33. this.name = name;
  34. }
  35. /**
  36. * @return the dirId
  37. */
  38. public ProductDir getDirId() {
  39. return dirId;
  40. }
  41. /**
  42. * @param dirId the dirId to set
  43. */
  44. public void setDirId(ProductDir dirId) {
  45. this.dirId = dirId;
  46. }
  47. /**
  48. * @Title: toString
  49. * @author zjh
  50. * @Desc:
  51. * @return
  52. * @see java.lang.Object#toString()
  53. */
  54. @Override
  55. public String toString() {
  56. return "Product [id=" + id + ", name=" + name + ", dirId=" + dirId
  57. + "]";
  58. }
  59. }
  60. @Entity
  61. @Table(name="tb_productDir")
  62. public class ProductDir {
  63. @Id
  64. @GeneratedValue(strategy=GenerationType.IDENTITY)
  65. private Long id;
  66. private String name;
  67. /**
  68. * @return the id
  69. */
  70. public Long getId() {
  71. return id;
  72. }
  73. /**
  74. * @param id the id to set
  75. */
  76. public void setId(Long id) {
  77. this.id = id;
  78. }
  79. /**
  80. * @return the name
  81. */
  82. public String getName() {
  83. return name;
  84. }
  85. /**
  86. * @param name the name to set
  87. */
  88. public void setName(String name) {
  89. this.name = name;
  90. }
  91. /**
  92. * @Title: toString
  93. * @author zjh
  94. * @Desc:
  95. * @return
  96. * @see java.lang.Object#toString()
  97. */
  98. @Override
  99. public String toString() {
  100. return "ProductDir [id=" + id + ", name=" + name + "]";
  101. }
  102. }

测试

  1. public class TestDemo {
  2. IProductDao iProductDao = new ProductDaoImpl();
  3. IProductDirDao iProductDirDao = new ProductDirDaoImpl();
  4. @org.junit.Test
  5. public void Test(){
  6. ProductDir productDir = new ProductDir();
  7. productDir.setName("食品");
  8. Product product = new Product();
  9. product.setName("牛肉");
  10. product.setDirId(productDir);
  11. iProductDirDao.save(productDir);
  12. iProductDao.save(product);
  13. }
  14. }

控制台打印

  1. Hibernate: insert into tb_productDir (name) values (?)
  2. Hibernate: insert into tb_product (dir_id, name) values (?, ?)

多对一

一的实体

一对多

一的实体

  1. @Entity
  2. public class Teacher {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. private Long id;
  6. private String name;
  7. @OneToMany
  8. //这里如果是要一对多关系模式的话,必须加上JoinColumn()注解,否则会生成一张关联表,那样的话就会被误解为多对多
  9. @JoinColumn(name="student_id")
  10. private List<Student> students = new ArrayList<>();
  11. /**
  12. * @return the id
  13. */
  14. public Long getId() {
  15. return id;
  16. }
  17. /**
  18. * @param id the id to set
  19. */
  20. public void setId(Long id) {
  21. this.id = id;
  22. }
  23. /**
  24. * @return the name
  25. */
  26. public String getName() {
  27. return name;
  28. }
  29. /**
  30. * @param name the name to set
  31. */
  32. public void setName(String name) {
  33. this.name = name;
  34. }
  35. /**
  36. * @return the students
  37. */
  38. public List<Student> getStudents() {
  39. return students;
  40. }
  41. /**
  42. * @param students the students to set
  43. */
  44. public void setStudents(List<Student> students) {
  45. this.students = students;
  46. }
  47. /**
  48. * @Title: toString
  49. * @author zjh
  50. * @Desc:
  51. * @return
  52. * @see java.lang.Object#toString()
  53. */
  54. @Override
  55. public String toString() {
  56. return "Teacher [id=" + id + ", name=" + name + "]";
  57. }
  58. }

多的实体

  1. @Entity
  2. public class Student {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. private Long id;
  6. private String name;
  7. /**
  8. * @return the id
  9. */
  10. public Long getId() {
  11. return id;
  12. }
  13. /**
  14. * @param id the id to set
  15. */
  16. public void setId(Long id) {
  17. this.id = id;
  18. }
  19. /**
  20. * @return the name
  21. */
  22. public String getName() {
  23. return name;
  24. }
  25. /**
  26. * @param name the name to set
  27. */
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31. /**
  32. * @Title: toString
  33. * @author zjh
  34. * @Desc:
  35. * @return
  36. * @see java.lang.Object#toString()
  37. */
  38. @Override
  39. public String toString() {
  40. return "Student [id=" + id + ", name=" + name + "]";
  41. }
  42. }

测试代码

  1. public class OneToManyTest {
  2. @Test
  3. public void testAdd(){
  4. Student student = new Student();
  5. student.setName("tom");
  6. Student student2 = new Student();
  7. student2.setName("jreey");
  8. Teacher teacher = new Teacher();
  9. teacher.setName("jack");
  10. teacher.getStudents().add(student);
  11. teacher.getStudents().add(student2);
  12. EntityManagerFactory entityManagerFactory = JPAUtils.getEntityManagerFactory();
  13. EntityManager createEntityManager = entityManagerFactory.createEntityManager();
  14. EntityTransaction transaction = createEntityManager.getTransaction();
  15. transaction.begin();
  16. createEntityManager.persist(teacher);
  17. createEntityManager.persist(student);
  18. createEntityManager.persist(student2);
  19. transaction.commit();
  20. createEntityManager.close();
  21. }
  22. }

日志

  1. Hibernate: insert into Teacher (name) values (?)
  2. Hibernate: insert into Student (name) values (?)
  3. Hibernate: insert into Student (name) values (?)
  4. Hibernate: update Student set student_id=? where id=?
  5. Hibernate: update Student set student_id=? where id=?

双向

一对多/多对一

发表评论

表情:
评论列表 (有 0 条评论,139人围观)

还没有评论,来说两句吧...

相关阅读