Search code examples
springspring-boothibernatejpa

EmbeddedId mappedBy Spring Boot 3.1.4 not working


Since the update to Spring Boot 3.1.4 i have an issue with mappedBy and EmbeddedId. It worked in all previous Sprint Boot version. Here an example:

Entity with mappedBy to EmbeddedId:

@Entity
@Getter
@Setter(AccessLevel.PROTECTED)
@Table(name = "STATUS")
public class Status {

    @Id
    private String id;

    @OneToMany(mappedBy = "id.globalId")
    private List<StatusLabel> labels;

}

Entity with EmbeddedId:

@Entity
@Getter
@Setter(AccessLevel.PROTECTED)
@Table(name = "STATUS_LABELS")
public class StatusLabel {

    @EmbeddedId
    private LabelGlobalId id;

    private String labelname;
    private String shortname;


}

EmbeddedId:

@Embeddable
@Getter
@Setter(AccessLevel.PROTECTED)
public class LabelGlobalId implements Serializable {

    private String globalId;
    private String langId;
}

Solution

  • To map an id or embedded id one should use the @MapsId. I suspect that the mappedBy in the relation worked by accident rather then by design.

    See also the relevant section in the documentation.

    @Entity
    @Getter
    @Setter(AccessLevel.PROTECTED)
    @Table(name = "STATUS")
    public class Status {
    
        @Id
        private String id;
    
        @OneToMany
        @MapsId("globalId")
        private List<StatusLabel> labels;
    
    }
    

    Something like that should do the trick.