Hibernate Tip: Customize the association mappings using a composite key

Hi, I’m Thorben Janssen from thoughts-on-java.org with a new Hibernate Tip. Today, I will show you how to customize the mapping of an association that references an entity with a composite primary key. But before we proceed, if you want to learn how to create your entity mappings with ease, build incredible efficient persistence layers with Hibernate and all types of other Java persistence related stuff, start now by subscribing and clicking the
bell, so you don’t miss anything. And if you have any topic or question related
to Hibernate that you want to learn more about, please tell me about it in the comment section Lets get into todays topic, One of my entities uses an @IdentityClass to map a composite primary key, and I need to map a one-to-many association to it. How can I change the names of the foreign key columns? If you don’t provide any additional information about your association mapping, Hibernate uses default names for the foreign
key columns. They follow the patternunderscore. That means that books_id is the default name for an association that’s mapped by the books attribute, and that references an entity with a primary key column called id. This approach is independent of the number of columns that form the primary key of the referenced entity. If it’s a composite primary key, Hibernate generates a default name for each of the identifying entity attributes. You can adapt this mapping using one or more
@JoinColumn annotations. Since Hibernate 5 and JPA 2.2, the @JoinColumn annotation is repeatable, and you can apply multiple of them to your association mapping attribute. If you’re using an older Hibernate version, you need a @JoinColumn annotation for each column and wrap them in a @JoinColumns annotation. Let’s take a look at a simple example in the IDE A Product gets identified by the combination of the manufacturerId and a manufacturer-specific productId. As requested in the question, I model the primary key using an @IdClass. If you want to learn more about this primarykey mapping or the mapping of composite primary keys in general, please take a look at my
Advanced Hibernate Online Training. The vital part of this Hibernate Tip is the mapping of the Review entity. The product attribute maps the association to the Product entity. Hibernate’s default mapping would expect that the columns product_productid and product_manufacturerid on the review table store the foreign key to a record in the product table. As described earlier, you can change this mapping by annotating the product attribute with one or more @JoinColumn annotations. I did that in the code snippet to tell Hibernate to use the columns p_id and m_id as the foreign key references to the productid and manufacturerid columns on the product table. When I use this entity to persist a new record, you can see that Hibernate uses the column names p_id and m_id instead of the default names. OK, that’s it for today. If you want to learn more about Hibernate, you should join the free Thoughts on Java Library. It gives you free access to a lot of member-only content like an ebook about the Java 8 support in Hibernate, a printable PDF version of this Hibernate Tip, lots of cheat sheets and a video course. I’ll add the link to it to the video description below. See you next week for a new Hibernate Tip and if you like today’s video, please give it thumbs up and subscribe below. Bye

Leave a Reply

Your email address will not be published. Required fields are marked *