Skip to content

JPA Buddy Ultimate Guide to Composite Ids in Jpa Entities implementation

Notifications You must be signed in to change notification settings

jdegand/collars

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Collars

This is based off this article from JPA Buddy and Spring PetClinic.

Thoughts

  • For H2 Database: need to have a test.db file in your main directory
  • The article doesn't provide a source code example. A lot of shortcuts are taken in the article and you have to fill in the gaps. The article devolved into another article showing the differences between EmbeddedId and IdClass.
  • I had some issues (Could not determine recommended JdbcType for com.example.collars.Pet error) because I swapped between EmbeddedId and IdClass implementations. It is best to follow to completion using only one implementation.
  • The JPA Buddy article prefers EmbeddedId over IdClass. This Baeldung article makes a lot of good points for IdClass. In most of my research of composite keys, EmbeddedId is preferred to IdClass but there are use cases for both.
  • IdClass requires code repetition but sql queries are simpler with IdClass. With embeddedId, you have to use nested queries in the sql. Getting values from the embedded class' fields may require chaining methods.
  • The JPA buddy article uses Pet (not pet.id) as part of the composite key. Need to investiate more if this is a good idea or not. It does allow you to query Pet fields. Otherwise, you can use @MapsId to get the values duplicated in the Collar entity.
  • The PetClinic repo uses the Query annotation for its Repo methods.
  • It is probably best practice not to mix and match and use Query for some methods and use a service for others.
  • With composite keys, the query method may actually be less complex than using a service and having to rely on url parameters.
  • I used EmbeddedId in this implementation.
  • I decided to remove a lot of the Pet Clinic implementation to just focus on the Composite key implementation.
  • I dumbed down the Pet entity to not use a PetType. Type now just takes a string.
  • Looking at the Spring PetClinic app, there is no id field in Pet entity itself as it is inherited from BaseEntity.
  • I have looked at many examples of Composite key implementations and I haven't really found any repos that have a complete CRUD. So my implementation here is not as complete as I would like since I haven't got much to compare and contrast. I believe you would add both ids to the url and create a CollarId with both ids. Then you could perform a getByCompositeKey method with the new CollarId you created. Then you could update the returned object's fields and re-save the entity.
  • CRUD could dependent on some design choices i.e. how and where (frontend/backend) you transform the form data.
  • The JPA buddy article glossed over the OneToOne association and whether the relationship should go in the Pet or Collar entity. This article shows how performance can be affected by the wrong placement.
  • To test the app, I thought about using CommandLineRunner on the main method or even using entityManager main method.
  • Ultimately, I added a DBInitializer since I have controller classes and then anyone could query the api to retrieve all seeded values.

Continued Development

  • File Structure
  • CORS
  • Full CRUD / Frontend

Useful Resources

  • Baeldung - jpa composite primary keys
  • Hello Koding - jpa hibernate composite primary key entity mapping example with mysql
  • YouTube - Spring Boot JPA ManyToMany
  • Blog - spring data jpa composite primary key
  • Amitph - spring data jpa embeddedId
  • Java Code Geeks - spring boot composite key example
  • Spring Docs - Spring Data Cassandra - Reference Documentation
  • YouTube - Complex Mapping - Composite Foreign Keys
  • CopyProgramming - how to map composite key in crud functionality
  • Vlad Mihalcea - best way to map a composite primary key with jpa and hibernate
  • YouTube - Association Mapping - One-To-One Embeddable
  • Stack Overflow - can someone please explain me mapsId in hibernate
  • Vlad Mihalcea - best way to map a OneToOne relationship with jpa and hibernate
  • YouTube - Hibernate Tip: Map a bidirectional one-to-one association with shared composite primary key
  • YouTube - The best way to map a @OneToOne relationship with JPA and Hibernate
  • Tutorials Point - how to create date object in java

About

JPA Buddy Ultimate Guide to Composite Ids in Jpa Entities implementation

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages