Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » EclipseLink 2.7.x OneToMany relation and composite id(parent id propagation when it's a part of a composite id)
EclipseLink 2.7.x OneToMany relation and composite id [message #1848085] Fri, 19 November 2021 11:47 Go to next message
alex alex is currently offline alex alexFriend
Messages: 1
Registered: November 2021
Junior Member
Short question:
Please help me to configure a relation between two entities, which will allow propagating a parent entity ID into a related entity composite key part.

Long question
We using EclipseLink 2.7.9.

I'm trying to configure a relation between two entities A and B.
A has regular id (a_id), which generates while saving.
B has a composite id (a_id, type)
Therefore each A may have some amount of B with different types.
Entity B is saving/updating only during a create/update operation of the A entity.

I experimented with a code and have found that:
- 'read only' field can be filled by the parent entity id without any problem. (insertable/updatable=false)
- If your 'relation field' is a part of the composite id it has to be annotated with @Id.
- Id-fields can't be 'read only'
- if the relationship between objects is configured with 'not-read-only-field' then the value of the parent entity id won't be propagated into it and the null will be saved DB.

But if I'll declare the bidirectional relation with @JoinColumn on both sides it almost works. EclipseLink adds the value into the field, but in the final 'insert-sql-query' this field will be mentioned twice and the DB will reject the request.

Call: INSERT INTO b_table (a_id, .... , a_id) VALUES (?, ... , ?)
bind => [61976e9cf7b620193be5c4b8, ... , 61976e9cf7b620193be5c4b8]

Here is a simplified entities code:
class A {
   String aId

   @JoinColumn(name = "a_id", nullable = false, updatable = false)
   @OneToMany(cascade = CascadeType.ALL)
   List<B> list;

class B {
  @Column(updatable=false, nullable=false)
  String type
   @JoinColumn(name = "a_id", nullable = false, updatable = false)
   A aInstance;

class CompositeId {
  String aInstance;
  String type;

While debugging the EclipseLink internals I've found that it :
- saves A entity (adds into the transaction) and starts to process its relations
- while processing the OneToMany relation (which annotated with @JoinColumn) in the OneToManyMapping.postInsert() method
it adds into dbRow object its id field+value and then all fields of the entity (with a duplicate).

Please help me to get rid of this column duplicate in the insert query or to reimplement the entities relation config in another way, which will work.
Re: EclipseLink 2.7.x OneToMany relation and composite id [message #1848421 is a reply to message #1848085] Wed, 01 December 2021 16:54 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 43
Registered: December 2021
This is a bi-directional OneToMany mapping, but by defining the join column, you have mapped it as a unidirectional, telling JPA that it must maintain the foreign key in B's table from A.

To make it bi-directional and avoid having two mappings to the same FK column, try:

   @OneToMany(mappedBy ="aInstance", cascade = CascadeType.ALL)
   List<B> list;

you might want to remove the updateable=false from your ID column annotations, as this is implied when you mark them as IDs - IDs cannot be changed in JPA without deleting and creating a new entity instance for the new ID values.

Best Regards,
Previous Topic:The class was compiled with an unsupported JDK.
Next Topic:class not found issues migrating to 3.0.2
Goto Forum:

Current Time: Tue Dec 05 16:34:29 GMT 2023

Powered by FUDForum. Page generated in 0.02148 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top