Advertisement
Guest User

Hibernate Composite Id Example

a guest
May 17th, 2021
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. /* ENTITY CLASSES */
  2. @Entity
  3. @Data
  4. @Table(name = "_Who")
  5. public class Who {
  6.     @Id
  7.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  8.     private Long id;
  9.  
  10.     private String name;
  11.  
  12.     @OneToMany(mappedBy = "who", fetch = FetchType.EAGER)
  13.     @JsonManagedReference
  14.     List<WhoWhatWhere> storage;
  15. }
  16.  
  17. @Entity
  18. @Data
  19. @Table(name = "_What")
  20. public class What {
  21.     @Id
  22.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  23.     private Long id;
  24.     private String thing;
  25. }
  26.  
  27. @Entity
  28. @Data
  29. @Table(name = "_Where")
  30. public class Where {
  31.     @Id
  32.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  33.     private Long id;
  34.     private String place;
  35. }
  36.  
  37. @Data
  38. @EqualsAndHashCode(onlyExplicitlyIncluded = true)
  39. @Entity
  40. @NoArgsConstructor
  41. @Table(name = "_WhoWhatWhere")
  42. public class WhoWhatWhere {
  43.     public WhoWhatWhere(Who who, What what, Where where) {
  44.         this.who = who;
  45.         this.what = what;
  46.         this.where = where;
  47.         this.setId(new WhoWhatWhereId(who.getId(), what.getId(), where.getId()));
  48.     }
  49.  
  50.     @EmbeddedId
  51.     WhoWhatWhereId id;
  52.  
  53.     @ManyToOne(fetch = FetchType.EAGER)
  54.     @JsonBackReference
  55.     @JoinColumn(name = "who_id", insertable = false, updatable = false)
  56.     private Who who;
  57.  
  58.     @ManyToOne(fetch = FetchType.EAGER)
  59.     @JoinColumn(name = "what_id", insertable = false, updatable = false)
  60.     private What what;
  61.  
  62.     @ManyToOne(fetch = FetchType.EAGER)
  63.     @JoinColumn(name = "where_id", insertable = false, updatable = false)
  64.     private Where where;
  65. }
  66.  
  67. @Embeddable
  68. @NoArgsConstructor
  69. @AllArgsConstructor
  70. @EqualsAndHashCode(onlyExplicitlyIncluded = true)
  71. public class WhoWhatWhereId implements Serializable {
  72.     @Column(name = "who_id")
  73.     Long whoId;
  74.     @Column(name = "what_id")
  75.     Long whatId;
  76.     @Column(name = "where_id")
  77.     Long whereId;
  78. }
  79.  
  80. /* REPOSITORIES */
  81. @Repository
  82. public interface WhoRepository extends PagingAndSortingRepository<Who, Long> {
  83.     Iterable<Who> findWhoByName (String name);
  84. }
  85. @Repository
  86. public interface WhatRepository extends PagingAndSortingRepository<What, Long> {
  87. }
  88. @Repository
  89. public interface WhereRepository extends PagingAndSortingRepository<Where, Long> {
  90. }
  91. @Repository
  92. public interface WhoWhatWhereRepository extends PagingAndSortingRepository<WhoWhatWhere, WhoWhatWhereId> {
  93. }
  94.  
  95. /* TEST CLASS */
  96. @SpringBootTest
  97. @Slf4j
  98. public class ThreeWayAssocTest {
  99.  
  100.     private final WhoRepository whoRepository;
  101.     private final WhatRepository whatRepository;
  102.     private final WhereRepository whereRepository;
  103.     private final WhoWhatWhereRepository whoWhatWhereRepository;
  104.  
  105.     @Autowired
  106.     public ThreeWayAssocTest(WhoRepository whoRepository, WhatRepository whatRepository, WhereRepository whereRepository, WhoWhatWhereRepository whoWhatWhereRepository) {
  107.         this.whoRepository = whoRepository;
  108.         this.whatRepository = whatRepository;
  109.         this.whereRepository = whereRepository;
  110.         this.whoWhatWhereRepository = whoWhatWhereRepository;
  111.     }
  112.  
  113.     @Test
  114.     public void attemptPersistence() {
  115.         /*
  116.         * the commented pieces can be used to do the initial inserts.  Later, fetch existing values so as not to fill
  117.         * up the database
  118.         */
  119.         Who who =
  120.         /*        new Who();
  121.         who.setName("Carl");
  122.         whoRepository.save(who);*/
  123.                 whoRepository.findById(1L).get();
  124.         What what =
  125.         /*        new What();
  126.         what.setThing("strawberry");
  127.         whatRepository.save(what);
  128.         what.setThing("salad");
  129.         whatRepository.save(what);*/
  130.                 whatRepository.findById(2L).get();
  131.         Where where =
  132.         /*        new Where();
  133.         where.setPlace("plate");
  134.         whereRepository.save(where);*/
  135.                 whereRepository.findById(1L).get();
  136.         WhoWhatWhere whoWhatWhere = new WhoWhatWhere(who, what, where);
  137.         whoWhatWhereRepository.save(whoWhatWhere);
  138.         LOGGER.debug("finished");
  139.     }
  140.  
  141.     @Test
  142.     public void testSerializing() throws JsonProcessingException {
  143.         Iterable<Who> examples = whoRepository.findWhoByName("Carl");
  144.         Who carl = examples.iterator().next();
  145.         LOGGER.debug("Carl: {}", carl);
  146.         LOGGER.debug("found some: \n {}", new ObjectMapper().writeValueAsString(examples));
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement