0

我有以下结构:

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;

}

现在我添加了一个新列:publicField 也添加了equalshashCode方法。

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();
    }
}

添加以下字段(数据库字段):

1. name = "Field1", publicField = '0'
2. name = "Field2", publicField = '0'
3. name = "Field3", publicField = '1'
4. name = "Field4", publicField = '1'

我如何进行更新:

....
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);

问题:

当我尝试更新 时field3field4被删除;当我尝试更新 时field1, 将field2被删除。

有人有什么建议吗?

4

2 回答 2

1

这听起来像一个错误。所以你需要:

于 2018-03-07T11:37:35.897 回答
0

最后我找到了问题解决方案

因为使用布尔值primitive而不是object那个问题。

解决方案是使用布尔包装器而不是原语。

private Boolean publicField;

现在可以正常工作而无需移除任何其他对象。

于 2018-03-07T19:33:34.533 回答