hibernate
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| hibernate [2020/12/27 20:35] – external edit 127.0.0.1 | hibernate [2023/07/14 12:42] (current) – [Transactional] skipidar | ||
|---|---|---|---|
| Line 219: | Line 219: | ||
| + | ===== Transactional ===== | ||
| + | Seems like when wiring entities, which are affected on both sides - it should happen in 1 transaction. | ||
| + | |||
| + | In Spring boot you need to annotate a method with '' | ||
| + | |||
| + | At a high level, Spring creates proxies for all the classes annotated with @Transactional, | ||
| + | |||
| + | https:// | ||
| + | |||
| + | |||
| + | https:// | ||
| + | |||
| + | <sxh java> | ||
| + | @Service | ||
| + | @Getter | ||
| + | public class EquipmentService { | ||
| + | |||
| + | @Autowired | ||
| + | private EquipmentRepository equipmentRepository; | ||
| + | |||
| + | @Transactional | ||
| + | public void updateEquipmentFeeds(ResourcePK equipmentFeeds, | ||
| + | Assert.isTrue(equipmentFeeds.getPartitionId().equals(equipmentTarget.getPartitionId()), | ||
| + | Equipment e1 = equipmentRepository.findById(equipmentFeeds).get(); | ||
| + | Equipment e2 = equipmentRepository.findById(equipmentTarget).get(); | ||
| + | e1.getFeeds().add(e2); | ||
| + | equipmentRepository.save(e1); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ==== ManyToMany with composite key to self ==== | ||
| + | |||
| + | <sxh java> | ||
| + | |||
| + | @ManyToMany(fetch = FetchType.EAGER, | ||
| + | @JoinTable(name=" | ||
| + | joinColumns={ | ||
| + | @JoinColumn( | ||
| + | name = " | ||
| + | referencedColumnName = " | ||
| + | @JoinColumn( | ||
| + | name = " | ||
| + | referencedColumnName = " | ||
| + | }) | ||
| + | private Set< | ||
| + | |||
| + | |||
| + | // Associations marked as mappedBy must not define database mappings like @JoinTable or @JoinColumn | ||
| + | @ManyToMany( cascade = CascadeType.ALL, | ||
| + | private Set< | ||
| + | </ | ||
hibernate.1609101328.txt.gz · Last modified: by 127.0.0.1
