Skip to main content

Composite Primary Keys, Custom Types and Attribute Converters

Learn Composite Primary Keys, Custom Types and Attribute Converters (Phase 9 · Advanced Hibernate (Advanced), Hibernate) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Composite primary keys: @EmbeddedId vs @IdClass • A composite key has several columns (a link table with (student_id, course_id) ). The key class must be Serializable with equals() and hashCode() and a no-arg constructor. • @EmbeddedId : the key is an @Embeddable object held as a single field ( enrollment.getId().getStudentId() ): cleaner, reusable in queries ( e.id.studentId ). @IdClass : the entity declares the key fields itself and points to a plain key class: queries use e.studentId ; handy with legacy schemas and for @Id @ManyToOne derived keys. Use @MapsId to derive key parts from...

More lessons in Phase 9 · Advanced Hibernate (Advanced)