Can someone help to find a solution with JPA and annotations if there are composite foreign keys to other tables where one of the columns is repeated across those foreign keys.
For hibernate XML configuration there is a workarround using the <formula> tag for the repeaded columns (not nice but ok). For JPA I see no solution so far.
The best and preferred solution would be to allow mixing of insert/update attributes on @JoinColumns (plural) annotations, but this results in a exception:
Quote:
org.hibernate.AnnotationException: Mixing insertable and non insertable columns in a property is not allowed
E.g. Having the following simple scenario:
There is a user table and a user can have one or more addresses and to distiguish between data of different clients a client_key has been added to all tables.
Code:
Table User:
client_key (pk)
user_id (pk)
address_id (fk)
second table:
Code:
Table Address
client_key (pk)
address_id (pk)
address line (property)
the coresponding join column annotation then looks like this:
Code:
@JoinColumns({
@JoinColumn(name = "client_key", referencedColumnName = "client_key", insertable = false, updatable = false),
@JoinColumn(name = "address_id", referencedColumnName = "address_id", insertable = true, updatable = true})
1. This scenario ends up in a AnnotationException as shown above because all insert and update attributes within the same @JoinColumns annotation must have the same value. (Why? I don't care, see my suggestions below).
2. Setting all values to insert/upate = true results also in a exception
Quote:
org.hibernate.MappingException: Repeated column in mapping for entity
and at least this configuration is wrong since I don't want to use both columns for insert/update. I only need the address_id when saving a user but not the repeated column client_key since it is already in the primary key.
I don't see the reason why to deny mixing insert/update=ture/false values within one compsite foreign keys except one: "What if someone adds a address of a different client than the user's client_key". Sure, this is a problem, but my answer to this is why not letting hibernate take care on this when a user object is saved. The logic to check would be quite simple:
Quote:
If a repeated column which exists in the primary key is used in a JoinColumn as well then do a logical check if the values are the same otherwise throw a integrity exception.
Sorry if there is a similar post somewhere in the forum. But I saw a lot of issues but every issue was a little bit different or no solution was provided etc.
Any help would be welcome. A patch would also be nice.
M