jakartaee / jakartaee/persistence

add mappedBy to @ManyToOne and @OneToOne

Open
#527 14 comments 0 reactions 0 assignees View on GitHub

@gavinking is already working on this.

Since Feb 27, 2026.

  • #973 by @gavinking — open
candidate-for-4.1
Dominant language
Java
Stars
268
Forks
78
Avg merge
1d 6h
Merged PRs (30d)
13

Description

The couple of weeks I've been thinking about the following sort of code, which is already possible in JPA:

@Entity
class Child {
     @Id 
     Long id;

     Long parentId;

     @ManyToOne 
     @Column(name="parentId", 
             insertable=false, updatable=false)
     Parent parent;

     ...
}

Here the parentId field and the parent association are mapped to the same column.

It seems to me that this sort of thing is not uncommon, but unfortunately that @Column annotation is quite clumsy.

We could make it quite a lot more elegant by allowing the following:

@Entity
class Child {
     @Id 
     Long id;

     Long parentId;

     @ManyToOne(mappedBy=Child.PARENT_ID)
     Parent parent;

     ...
}

This is quite analogous to the use of mappedBy in @OneToMany and @ManyToMany, with the difference that here mappedBy points to a different field of the current entity.

An argument against this features is that it overlaps with @MapsId, and that's true, I can use it this way:

@Entity
@IdClass(ChildId.class)
class Child {
     @Id 
     Long id;

     @Id
     Long parentId;

     @ManyToOne(mappedBy=Child.PARENT_ID)
     Parent parent;

     ...
}

or this way:

@Entity
class Child {
     @Id 
     Long id;

     @OneToOne(mappedBy=Child.ID)
     Parent parent;

     ...
}

to do things that are already possible with @MapsId.

On the other hand, I find @MapsId to be quite confusing in general, and sort-of backwards compared to my intuition. So I actually prefer the above code to the equivalent thing with @MapsId.

Reactions?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the proposed @ManyToOne and @OneToOne examples in this issue, then review the linked pull request #973 and the existing @MapsId and mappedBy behavior. Done means resolving the API design and documenting or implementing an agreed way to map these associations to another field of the same entity.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.