How to synchronize bidirectional entity associations with JPA and Hibernate

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!

Introduction

While answering this StackOverflow question, I realized that it’s a good idea to summarize how various bidirectional associations should be synchronized when using JPA and Hibernate.

Therefore, in this article, you are going to learn how and also why you should always synchronize both sides of an entity relationship, no matter if it’s @OneToMany, @OneToOne or @ManyToMany.

One-To-Many

Let’s assume we have a parent Post entity which has a bidirectional association with the PostComment child entity:

The PostComment entity looks as follows:

@Entity(name = "PostComment")
@Table(name = "post_comment")
public class PostComment {

    @Id
    @GeneratedValue
    private Long id;

    private String review;

    @ManyToOne(
        fetch = FetchType.LAZY
    )
    @JoinColumn(name = "post_id")
    private Post post;

    //Getters and setters omitted for brevity

    @Override
    public boolean equals(Object o) {
        if (this == o) 
            return true;
            
        if (!(o instanceof PostComment)) 
            return false;
            
        return 
            id != null && 
           id.equals(((PostComment) o).getId());
    }
    @Override
    public int hashCode() {
        return 31;
    }
}

There are several things to notice in the PostComment entity mapping above.

First, the @ManyToOne association uses the FetchType.LAZY strategy because by default @ManyToOne and @OneToOne associations use the FetchType.EAGER strategy which is bad for performance.

Second, the equals and hashCode methods are implemented so that we can use safely use the entity identifier, as explained in this article.

The Post entity is mapped as follows:

@Entity(name = "Post")
@Table(name = "post")
public class Post {

    @Id
    @GeneratedValue
    private Long id;

    private String title;

    @OneToMany(
        mappedBy = "post", 
        cascade = CascadeType.ALL, 
        orphanRemoval = true
    )
    private List<PostComment> comments = new ArrayList<>();

    //Getters and setters omitted for brevity

    public void addComment(PostComment comment) {
        comments.add(comment);
        comment.setPost(this);
    }

    public void removeComment(PostComment comment) {
        comments.remove(comment);
        comment.setPost(null);
    }
}

The comments @OneToMany association is marked with the mappedBy attribute which indicates that the @ManyToOne side is responsible for handling this bidirectional association.

However, we still need to have both sides in sync as otherwise, we break the Domain Model relationship consistency, and the entity state transitions are not guaranteed to work unless both sides are properly synchronized.

For this reason, the Post entity defines the addComment and removeComment entity state synchronization methods.

So, when you add a PostComment, you need to use the addComment method:

Post post = new Post();
post.setTitle("High-Performance Java Persistence");

PostComment comment = new PostComment();
comment.setReview("JPA and Hibernate");
post.addComment(comment);

entityManager.persist(post);

And, when you remove a PostComment, you should use the removeComent method as well:

Post post = entityManager.find(Post.class, 1L);
PostComment comment = post.getComments().get(0);

post.removeComment(comment);

For more details about the best way to map a @OneToMany association, check out this article.

One-To-One

For the one-to-one association, let’s assume the parent Post entity has a PostDetails child entity as illustrated in the following diagram:

The child PostDetails entity looks like this:

@Entity(name = "PostDetails")
@Table(name = "post_details")
public class PostDetails {

    @Id
    private Long id;

    @Column(name = "created_on")
    private Date createdOn;

    @Column(name = "created_by")
    private String createdBy;

    @OneToOne(fetch = FetchType.LAZY)
    @MapsId
    private Post post;
    
    //Getters and setters omitted for brevity
}

Notice that we have set the @OneToOne fetch attribute to FetchType.LAZY, for the very same reason we explained before. We are also using @MapsId because we want the child table row to share the Primary Key with its parent table row meaning that the Primary Key is also a Foreign Key back to the parent table record.

The parent Post entity looks as follows:

@Entity(name = "Post")
@Table(name = "post")
public class Post {

    @Id
    @GeneratedValue
    private Long id;

    private String title;

    @OneToOne(
        mappedBy = "post", 
        cascade = CascadeType.ALL, 
        orphanRemoval = true, 
        fetch = FetchType.LAZY
    )
    private PostDetails details;

    //Getters and setters omitted for brevity

    public void setDetails(PostDetails details) {
        if (details == null) {
            if (this.details != null) {
                this.details.setPost(null);
            }
        }
        else {
            details.setPost(this);
        }
        this.details = details;
    }
}

The details @OneToOne association is marked with the mappedBy attribute which indicates that the PostDetails side is responsible for handling this bidirectional association.

The setDetails method is used for synchronizing both sides of this bidirectional association and is used both for adding and removing the associated child entity.

So, when we want to associate a Post parent entity with a PostDetails, we use the setDetails method:

Post post = new Post();
post.setTitle("High-Performance Java Persistence");

PostDetails details = new PostDetails();
details.setCreatedBy("Vlad Mihalcea");

post.setDetails(details);

entityManager.persist(post);

The same is true when we want to dissociate the Post and the PostDetails entity:

Post post = entityManager.find(Post.class, 1L);

post.setDetails(null);

For more details about the best way to map a @OneToOne association, check out this article.

Many-To-Many

Let’s assume the Post entity forms a many-to-many association with Tag as illustrated in the following diagram:

The Tag is mapped as follows:

@Entity(name = "Tag")
@Table(name = "tag")
public class Tag {

    @Id
    @GeneratedValue
    private Long id;

    @NaturalId
    private String name;

    @ManyToMany(mappedBy = "tags")
    private Set<Post> posts = new HashSet<>();

    //Getters and setters omitted for brevity

    @Override
    public boolean equals(Object o) {
        if (this == o) 
            return true;
            
        if (!(o instanceof Tag))
            return false;
        
        Tag tag = (Tag) o;
        return Objects.equals(name, tag.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}

Notice the use of the @NaturalId Hibernate-specific annotation which is very useful for mapping business keys.

Because the Tag entity has a business key, we can use that for implementing equals and hashCode as explained in this article.

The Post entity is then mapped as follows:

@Entity(name = "Post")
@Table(name = "post")
public class Post {

    @Id
    @GeneratedValue
    private Long id;

    private String title;

    public Post() {}

    public Post(String title) {
        this.title = title;
    }

    @ManyToMany(
        cascade = { 
            CascadeType.PERSIST, 
            CascadeType.MERGE
        }
    )
    @JoinTable(name = "post_tag",
        joinColumns = @JoinColumn(name = "post_id"),
        inverseJoinColumns = @JoinColumn(name = "tag_id")
    )
    private Set<Tag> tags = new LinkedHashSet<>();

    //Getters and setters omitted for brevity   

    public void addTag(Tag tag) {
        tags.add(tag);
        tag.getPosts().add(this);
    }

    public void removeTag(Tag tag) {
        tags.remove(tag);
        tag.getPosts().remove(this);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) 
            return true;
        
        if (!(o instanceof Post)) return false;
        
        return id != null && id.equals(((Post) o).getId());
    }

    @Override
    public int hashCode() {
        return 31;
    }
}

The tags @ManyToMany association is marked with the mappedBy attribute which indicates that the Tag side is responsible for handling this bidirectional association.

The addTag and removeTag methods are used for synchronizing the bidirectional association. Because we rely on the remove method from the Set interface, both the Tag and Post must implement equals and hashCode properly. While Tag can use a natural identifier, the Post entity does not have such a business key. For this reason, we used the entity identifier to implement these two methods, as explained in this article.

To associate the Post and Tag entities, we can use the addTag method like this:

Post post1 = new Post("JPA with Hibernate");
Post post2 = new Post("Native Hibernate");

Tag tag1 = new Tag("Java");
Tag tag2 = new Tag("Hibernate");

post1.addTag(tag1);
post1.addTag(tag2);

post2.addTag(tag1);

entityManager.persist(post1);
entityManager.persist(post2);

To dissociate the Post and Tag entities, we can use the removeTag method:

Post post1 = entityManager
.createQuery(
    "select p " +
    "from Post p " +
    "join fetch p.tags " +
    "where p.id = :id", Post.class)
.setParameter( "id", postId )
.getSingleResult();

Tag javaTag = entityManager.unwrap(Session.class)
.bySimpleNaturalId(Tag.class)
.getReference("Java");

post1.removeTag(javaTag);

For more details about the best way to map a @ManyToMany association, check out this article.

That’s it!

If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.

Seize the deal! 50% discount. Seize the deal! 50% discount.

Conclusion

Whenever you are using a bidirectional JPA association, it is mandatory to synchronizing both ends of the entity relationship.

Not only that working with a Domain Model, which does not enforce relationship consistency, is difficult and error prone, but without synchronizing both ends of a bidirectional association, the entity state transitions are not guaranteed to work.

So, save yourself some trouble and do the right thing.

FREE EBOOK

13 Comments on “How to synchronize bidirectional entity associations with JPA and Hibernate

  1. Vlad,

    Thanks sketching this all out. However in many of the entities I deal with, business logic dictates that the child entity cannot change owners once it has been created. In your examples you use an explicit setter, which violates aforementioned business logic and allows changes to the parent after creation. How would you synchronize the association in this case? I’ve thought about doing it from the constructor of the child using an addChild method on the parent – but having an addChild method on the parent that cannot set the child’s parent property seems like it could cause un-synchronized data.

    • The setters are needed since the main purpose of Hibernate is to propagate changes to the database. If your domain model is immutable, you only need SQL queries, not Hibernate.

  2. Hi Vlad, Thanks a lot for this post and the amazing book, I have a question can we make both entities owners for ManyToMany relationship because I want to persist on both sides on the entities
    e.g
    Post[tag1,tag2,tag3]
    Tag[post1,post2,post3]

    I added @JoinTable on both sides e.g Post & Tag entites and removed the mappedBy flag

    • Thanks. In your case, you just need to set cascade properly. That doesn’t have to do anything with mappedBy, which should be added to one side. Removing it, it’s wrong.

  3. Hi Vlad,

    I’m introduced to your blogs by my colleague. I like your articles as they are well explained and to the point.

    In this article, in one-to-one section, in setDetails method of Post class, is it necessary to explicitly set this.details.setPost to null when this.details is not null? I mean when the session is flushed, doesn’t hibernate takes care of deleting the associated details object?

    • Yes, it is necessary. Otherwise, there is no guarantee that the link will not be set to the old association during flush.

  4. Hi Vlad. Thank you for this explanation. I am struggling with this subject a while and can’t get the code stable. I need to have the database id of a newly created child, since it is required to display it in the UI in my application. If I save the child I’ll get the saved entity with its new id, but if I save/persist the parent side, then how do I get the id of the child?

  5. Actually, synchronizing relationships may, in some cases and implementations have to be, like you say, business logic, but it in most cases it should not be. Why? Because associations are mapped and persisted by ORM that has to understand and maintain their implications.

    Yes, there are certain requirements that have to be met for this to work, such as sufficient mapping/annotation information, ability to intercept what the code does and assurance that there is at most one Java instance mapped to any one entity at a time (in a single transaction). However, once these are met, the risk of subtle (and very much not subtle) bugs is far greater if we leave it to business logic to synchronize associations.

    Why? Well, for one, this is mostly about large products, not small. In large products we rely on separation of concern, encapsulation and simplicity to reduce the risk of developers making mistakes. They are meant to think about their domain of responsibility mostly. They WILL forget to synchronize associations if it is up to them and this will not be discovered immediately. It will often “bite” someone else and their code when they, unbeknownst to them, receive an out-of-sync object.

    Who/what knows what needs to end up where? ORM. There are cases when this may sound like not true. Say that there is an association that is an (ordered) list on one end and a simple 0..1 reference on the other. Setting that reference does not supply sufficient information as to where should it be positioned in that list on the other side. W.r.t. we could say (not a comprehensive list of options):

    This is an error and should never be done on its own. There shouldn’t even be such a method. If anything attempts it, it should immediately fail so it gets noticed quickly… but the framework must still be able to update this by reflecting the change on the “owner” side, in this case the list.
    There could be a rule that does this. This is easily implemented as a setter that really modifies the other side as opposed to directly. Or it could be annotated some way. Or something else. But it can be done.

    These cases are not only solvable as per above but also rather rare compared to simple cases that can be trivially dealt with.

    Also consider what it means for business logic to do synchronization properly, both ways:

    Ensure that bi-directional sync does not become an infinite call loop “ping-pong”.
    Potentially understand how the relationship is mapped so that it can perform all required structure updates.
    Check if any/all “ends” are initialized/loaded. If they are update them. If not loaded but are derived from other sides, ensure that session.flush() occurs regardless of the flush mode – potential performance hit and an excuse to “temporarily” forget about the sync… until it “bites” someone else. This flush, in turn, may fail if the “setup” isn’t complete yet, leaving the business logic developer with no real options.
    Deal with a possible inconsistency due to the fact that some code was developed in the past without or with defective sync logic (as opposed to well-tested centralized version).

    … this quickly turns into rather nasty code that must be repeated for every end of every association.

  6. Hi Vlad, can you please elaborate on “we break the Domain Model relationship consistency”. What kind of problems can arise if, lets say, I will just add PostComment to a Post without adding a Post to a PostComment?

    • Any business logic that depends on the presence or lack of that association will break. And not just the business logic, but the UI will also render wrong results which can further lead to wrong decisions.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.