How to change the @OneToOne shared primary key column name with JPA and Hibernate

Introduction In this article, we are going to see how you can change the @OneToOne shared primary key column name when using JPA and Hibernate. This has been a recurrent theme when answering questions about Hibernate or during my High-Performance Java Persistence training. As previously explained, the one-to-one database table relationship requires the Primary Key to be shared among the parent and the child tables. Unfortunately, just adding the JPA @OneToOne annotation in the child entity does not render a true one-to-one table relationship since a separate Foreign Key column will be… Read More

How to map a JPA @ManyToOne relationship to a SQL query using the Hibernate @JoinFormula annotation

Introduction Someone asked me to answer the following StackOverflow question, and, because the question is very interesting from an SQL perspective, I decided to turn the answer it into a blog post. In this article, we are going to see how to map a JPA @ManyToOne association to the result of a SQL query using the Hibernate-specific @JoinFormula annotation.

How to map a @ManyToOne association using a non-Primary Key column with JPA and Hibernate

Introduction While answering questions on the Hibernate forum, I stumbled on the following question about using the @ManyToOne annotation when the Foreign Key column on the client side references a non-Primary Key column on the parent side. In this article, you are going to see how to use the @JoinColumn annotation in order to accommodate non-Primary Key many-to-one associations.

The best way to map a @OneToMany relationship with JPA and Hibernate

Introduction In a relational database system, a one-to-many association links two tables based on a Foreign Key column so that the child table record references the Primary Key of the parent table row. As straightforward as it might be in a RDBMS, when it comes to JPA, the one-to-many database association can be represented either through a @ManyToOne or a @OneToMany association since the OOP association can be either unidirectional or bidirectional. The @ManyToOne annotation allows you to map the Foreign Key column in the child entity mapping so that the child… Read More