I have the following structure:
public class Profile {
...
@ElementCollection(targetClass = ProfileFieldImpl.class, fetch = FetchType.EAGER)
@CollectionTable(name = "profileField", joinColumns = @JoinColumn(name = "profileId"))
@OrderColumn(name = "fieldName")
private Set<ProfileField> fields;
}
@Embeddable
public class ProfileFieldImpl {
@AttributeOverride(name = "id", column = @Column(name = "rofileFieldId", nullable = false))
private Long profileFieldId;
private String name;
}
Now i'm adding a new column: publicField
Added also the equals
and hashCode
mothods.
public class ProfileFieldImpl {
@AttributeOverride(name = "id", column = @Column(name = "rofileFieldId", nullable = false))
private Long profileFieldId;
private String name;
private boolean publicField;
@Override
public final boolean equals(final Object o) {
if (o == this) {
return true;
} else if (o == null || !(o instanceof ProfileFieldImpl)) {
return false;
} else {
final ProfileFieldImpl other = (ProfileFieldImpl) o;
return EqualsUtil.equalsNullable(getName(), other.getName()) && EqualsUtil.equalsNullable(isPublicField(), other.isPublicField()));
}
}
@Override
public final int hashCode() {
return HashCodeUtil.seed(HashCodeUtil.SEED_13).with(name).with(publicField).hashCode();
}
}
Adding the following fields (Database fields):
1. name = "Field1", publicField = '0'
2. name = "Field2", publicField = '0'
3. name = "Field3", publicField = '1'
4. name = "Field4", publicField = '1'
How I do the update:
....
final ProfileImpl profile = getEntityManager().find(Profile.java, profileId, LockModeType.NONE);
final Set<ProfileField> fields = profile.getFields();
fields.stream().filter(field -> field.getName().equals(currentFieldName)).forEach(field -> {
field.setName(updatedProfileField.getName());
field.setPublicField(updatedProfileField.isPublicField());
});
getEntityManager().merge(profile);
The Issue:
When I'm trying to update the field3
, the field4
is deleted;
When I'm trying to update the field1
, the field2
is deleted.
Do someone have any suggestion?
Finally I've found the issue and also the solution.
Because of using boolean as primitive
instead of object
was that issue.
Solution was to use the Boolean wrapper instead of the primitive.
private Boolean publicField;
Now is working properly without removing any other object.