I am looking at a very simple entity with a Map
@ElementCollection
.
The entity:
@Entity
@RequiredArgsConstructor @NoArgsConstructor @Data
public class Attribute {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NonNull
private String name;
@ElementCollection
private Map<Integer, AttributeValue> attributes = new HashMap<>();
}
The Embeddable
used as the map value:
@Embeddable
@RequiredArgsConstructor @NoArgsConstructor(access = PRIVATE) @Data
public class AttributeValue {
@NonNull
private String name;
}
When I try to update the entity with a new map entry where there is already an equal persisted value (with the same name
in this case) the map entry is not inserted.
This test reproduces this issue:
@Test
public void should_persist_attribute_with_same_value() {
Attribute name = new Attribute("name");
name.getAttributes().put(1, new AttributeValue("1"));
name.getAttributes().put(2, new AttributeValue("2"));
name = attributeRepository.saveAndFlush(name);
name.getAttributes().put(3, new AttributeValue("2")); //this value is already in the map and is not inserted
name.getAttributes().put(4, new AttributeValue("4"));
name = attributeRepository.saveAndFlush(name);
entityManager.clear();
//the assertion fails and finds only three items (with key 1,2 and 4)
then(attributeRepository.findOne(name.getId()).getAttributes()).hasSize(4);
}
It seems that there is a bug in eclipselink that ignores map values that are already used with another map key in the map.
I also tried with hibernate - there the same code works as expected.
Any ideas?
I am using:
- spring-boot 1-4-0.M2
- spring-data-jpa 1.10.1.RELEASE
- eclipselink 2.6.1