表1 S_people id主键 与表2 S_card一对多
@Entity
@Table(name="s_people")
public class S_people {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@SequenceGenerator(name = "s_people_seq",sequenceName = "s_people_seq",allocationSize = 1)private int id;private String username;private String age;@OneToMany(cascade = CascadeType.ALL,fetch=FetchType.EAGER)// 创建中间表,存储映射关系@JoinTable(name="s_people_card",joinColumns={@JoinColumn(name="p_id")},inverseJoinColumns={@JoinColumn(name="c_id")})private List<S_card> cards;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public List<S_card> getCards() {return cards;}public void setCards(List<S_card> cards) {this.cards = cards;}
}
表2
@Entity
@Table(name="s_card")
public class S_card {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@SequenceGenerator(name = "s_card_seq",sequenceName = "s_card_seq",allocationSize = 1)private int id;private String num;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getNum() {return num;}public void setNum(String num) {this.num = num;}
}
JPA运行是会自动根据表一@JoinTable()里的内容建立s_people_card表,里面存放的是两张表段的id用以表示关联
联级完成,
相关联级使用得controller
@ApiOperation(value = "JPA联级OTM插入测试", notes = "JPA联级OTM插入测试")@PostMapping(value = "OMtest")public ResponseEntity<Map> OTMJPAInsert() {S_card c1 = new S_card();c1.setNum("1");S_card c2 = new S_card();c2.setNum("2");S_people s = new S_people();s.setAge("2");s.setUsername("2");List<S_card> l = new ArrayList<>();l.add(c1);l.add(c2);s.setCards(l);try {omPeopleRepository.saveAndFlush(s);} catch (Exception e) {return ResponseEntity.ok(getPublicBackValueDir(false, new HashMap()));}return ResponseEntity.ok(getPublicBackValueDir(true, new HashMap()));}@ApiOperation(value = "JPA联级OTM删除测试", notes = "JPA联级OTM插入测试")@DeleteMapping(value = "OMDtest")public ResponseEntity<Map> OTMJPADelete(@ApiParam(name = "id") @RequestParam int id) {try {omPeopleRepository.deleteById(id);
// omCardRepository.deleteById(id);
// S_card s=new S_card();
// s.setId(3);
// s.setNum("888");
// S_people p=new S_people();
// p.setId(3);
// p.setUsername("8");
// p.setAge("7");
// omCardRepository.saveAndFlush(s);
// omPeopleRepository.saveAndFlush(p);} catch (Exception e) {return ResponseEntity.ok(getPublicBackValueDir(false, new HashMap()));}return ResponseEntity.ok(getPublicBackValueDir(true, new HashMap()));}@ApiOperation(value = "JPA联级OTM查询测试", notes = "JPA联级OTM查询测试")@GetMapping(value = "OMSelect")public ResponseEntity<Map> OTMJPASelect(@ApiParam(name = "id") @RequestParam int id) {List<S_people> list = omPeopleRepository.findAllById(id);Map<String, Object> result = new HashMap<>();if(list.isEmpty()){return ResponseEntity.ok(getPublicBackValueDir(false, result));}for (S_people p : list) {result.put("data", p);}return ResponseEntity.ok(getPublicBackValueDir(true, result));}
Repository
public interface OMCardRepository extends JpaRepository<S_card, Integer> {}
除了删除操作,分别对两张表单独进行修改操作都是被允许的