User Tools

Site Tools


hibernate

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
hibernate [2020/04/29 09:55] skipidarhibernate [2023/07/14 12:42] (current) – [Transactional] skipidar
Line 139: Line 139:
 </sxh> </sxh>
  
 +Results in
  
-campus_campuspart +**campuses** 
-|campus_id|campuspart_id|+^dtype^id^object_type^name^ 
 +|ECampusPart|497ffe9a-51bb-4ab4-bc3d-b8923d620703|CampusPart|ViennaSmart City Campus front part| 
 + 
 + 
 +**campusparts** 
 +^dtype^id^object_type^name^ 
 +|ECampus|dbadab87-7b6d-4e8b-b2c8-0663d68dfe7b|Campus|ViennaSmart City Campus| 
 + 
 + 
 +**campus_campuspart** 
 +^campus_id^campuspart_id
 +|497ffe9a-51bb-4ab4-bc3d-b8923d620703|dbadab87-7b6d-4e8b-b2c8-0663d68dfe7b|
  
  
Line 147: Line 159:
 == Owning-side - with embedded foreign key == == Owning-side - with embedded foreign key ==
  
-The alternative to embed the Foreign key - into the CampusPart table:+Omitting the "JoinTable" results in embedding the Foreign key **campus_id** - into the **CampusPart** table
  
 <sxh java> <sxh java>
Line 162: Line 174:
  
 Results in  Results in 
-campus 
-|id| | 
  
-campuspart +**campuses** 
-|id| |+^dtype^id^object_type^name^campus_id^ 
 +|ECampusPart|497ffe9a-51bb-4ab4-bc3d-b8923d620703|CampusPart|ViennaSmart City Campus front part|dbadab87-7b6d-4e8b-b2c8-0663d68dfe7b| 
 + 
 + 
 +**campusparts** 
 +^dtype^id^object_type^name^ 
 +|ECampus|dbadab87-7b6d-4e8b-b2c8-0663d68dfe7b|Campus|ViennaSmart City Campus| 
  
  
Line 192: Line 209:
  
  
 +===== Warning: bidirectional relationships ======
  
 +ManyToMany relationships can lead to inconsitancies when
 +  * settings links back
 +  * there are multiple entities linking, back to parents 
 +  * entities have inconsistant list of members
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/2020-05-05_12-01-00.png}}
 +
 +
 +
 +===== 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 ''@Transactional''
 +
 +At a high level, Spring creates proxies for all the classes annotated with @Transactional, either on the class or on any of the methods. The proxy allows the framework to inject transactional logic before and after the running method, mainly for starting and committing the transaction.
 +
 +https://www.baeldung.com/transaction-configuration-with-jpa-and-spring
 +
 +
 +https://thorben-janssen.com/transactions-spring-data-jpa/
 +
 +<sxh java>
 +@Service
 +@Getter
 +public class EquipmentService {
 +
 +    @Autowired
 +    private EquipmentRepository equipmentRepository;
 +     
 +    @Transactional
 +    public void updateEquipmentFeeds(ResourcePK equipmentFeeds, ResourcePK equipmentTarget) {
 +        Assert.isTrue(equipmentFeeds.getPartitionId().equals(equipmentTarget.getPartitionId()), "Only allow feeds in same partition");
 +        Equipment e1 = equipmentRepository.findById(equipmentFeeds).get();
 +        Equipment e2 = equipmentRepository.findById(equipmentTarget).get();
 +        e1.getFeeds().add(e2);
 +        equipmentRepository.save(e1);
 +    } 
 +}
 +</sxh>
 +
 +
 +
 +==== ManyToMany with composite key to self ====
 +
 +<sxh java>
 +
 +    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
 +    @JoinTable(name="FeedsRel",
 +            joinColumns={
 +                    @JoinColumn(
 +                            name = "feedsrel_child_id",
 +                            referencedColumnName = "id"),
 +                    @JoinColumn(
 +                            name = "feedsrel_child_partitionId",
 +                            referencedColumnName = "partitionId")
 +            })
 +    private Set<Equipment> feeds = new HashSet<Equipment>();
 +
 +
 +    // Associations marked as mappedBy must not define database mappings like @JoinTable or @JoinColumn
 +    @ManyToMany( cascade = CascadeType.ALL, mappedBy="feeds", fetch = FetchType.EAGER)
 +    private Set<Equipment> fedBy = new HashSet<Equipment>();
 +</sxh>
hibernate.1588154144.txt.gz · Last modified: (external edit)