I have an entity with the following mapping defined:
@ElementCollection(fetch=FetchType.LAZY, targetClass=MyEntityTranslations.class)
@CollectionTable(name="MY_ENTITY_TRANSLATIONS", joinColumns=@JoinColumn(name="PARENT_ID"))
@MapKeyColumn(name="LOCALE")
private Map<String, MyEntityTranslations> translations;
The MyEntityTranslations
class looks like this:
@Embeddable
public class MyEntityTranslations
{
@Column(name="NAME")
private String name;
@Column(name="DESCRIPTION")
private String description;
public MyEntityTranslations()
{
}
//getters and setters
}
The MY_ENTITY_TRANSLATIONS table looks like this:
CREATE TABLE MY_ENTITY_TRANSLATIONS
(
parent_id bigint NOT NULL,
locale character varying NOT NULL,
name character varying,
description character varying,
CONSTRAINT my_entity_translations_pkey PRIMARY KEY (parent_id, locale)
);
Everything works fine and as expected, but when I update the name or description in MyEntityTranslations
it is doing a DELETE followed by an INSERT instead of just an UPDATE.
I thought maybe adding equals()
and hashCode()
methods to MyEntityTranslations
would allow JPA to know if it should simply be updated. However, I quickly realized I don't have the information necessary in MyEntityTranslations
to properly override those methods.
After googling around about this issue, I found plenty of places for this problem with a List
, with the solution being adding an @OrderColumn
annotation or changing it to a Set
. However, I couldn't find anything about this for a Map
.
Really, after a record is inserted into the MY_ENTITY_TRANSLATIONS table, it will never need to be deleted unless the entity with the id equal to the PARENT_ID column is deleted. Is there anything I can do with either the entity or the embeddable that will allow JPA to always do UPDATEs instead of the DELETE/INSERT behavior?
In case it makes a difference, I'm using PostgreSQL, Spring Data JPA, and EclipseLink.