实验一 JPA基本关联映射实验
双向 One to Many
One端使用@OneToMany声明与Many端关系,设置mappedBy 属性声明在另一端映射的属性名称
Many端使用@ManyToOne声明与One端关系
User.java + Address.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 @Getter @Setter @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; @OneToMany(mappedBy = "user") private List<Address> addresses; @Column(columnDefinition = "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP", insertable = false, updatable = false) private LocalDateTime insertTime; public User (String name) { this .name = name; } } @Entity @Getter @Setter public class Address { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String detail; @ManyToOne private User user; @Column(columnDefinition = "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP", insertable = false, updatable = false) private LocalDateTime insertTime; public Address (String detail) { this .detail = detail; } }
UserRepository.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @Repository @Transactional public class UserRepository { @PersistenceContext private EntityManager em; public void addUserAddress () { User user = new User("bo" ); em.persist(user); Address address1 = new Address("956" ); address1.setUser(user); em.persist(address1); Address address2 = new Address("925" ); address2.setUser(user); em.persist(address2); } }
将 many to many 关系拆分成一个独立的实体类,保存关系描述属性
Course.java + Student.java + Elective.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 @Entity @Getter @Setter public class Course { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; @OneToMany(mappedBy = "course") private List<Elective> electiveList; @Column(columnDefinition = "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP", insertable = false, updatable = false) private LocalDateTime insertTime; public Course (String name) { this .name = name; } } @Entity @Getter @Setter public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; @OneToMany(mappedBy = "student") private List<Elective> electiveList; @Column(columnDefinition = "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP", insertable = false, updatable = false) private LocalDateTime insertTime; public Student (String name) { this .name = name; } } @Entity @Getter @Setter public class Elective { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) int id; @ManyToOne private Student student; @ManyToOne private Course course; @Column(columnDefinition = "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP", insertable = false, updatable = false) private LocalDateTime insertTime; }
ElectiveRepository.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 @Repository @Transactional public class ElectiveRepository { @PersistenceContext private EntityManager em; public void addElective () { Student stud1 = new Student("zhanyeye" ); Student stud2 = new Student("xiaoming" ); em.persist(stud1); em.persist(stud2); Course c1 = new Course("math" ); Course c2 = new Course("english" ); em.persist(c1); em.persist(c2); Elective e1 = new Elective(); e1.setCourse(c1); e1.setStudent(stud1); Elective e2 = new Elective(); e2.setCourse(c1); e2.setStudent(stud2); em.persist(e1); em.persist(e2); } }
实体对象状态实验