EclipseLink 2.7.x OneToMany relation and composite id [message #1848085] |
Fri, 19 November 2021 11:47 |
alex alex 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.
Quote:
Call: INSERT INTO b_table (a_id, .... , a_id) VALUES (?, ... , ?)
bind => [61976e9cf7b620193be5c4b8, ... , 61976e9cf7b620193be5c4b8]
Here is a simplified entities code:
class A {
@Id
@GeneratedValue
@Column(name="a_id)
String aId
@JoinColumn(name = "a_id", nullable = false, updatable = false)
@OneToMany(cascade = CascadeType.ALL)
List<B> list;
}
@IdClass(CompositeId.class)
class B {
@Id
@Column(updatable=false, nullable=false)
String type
@Id
@ManyToOne
@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.
|
|
|
|
Powered by
FUDForum. Page generated in 0.03603 seconds