gpt4 book ai didi

java - Hibernate 使用 @CollectionTable 删除更新时具有相同 boolean 值的 set 对象

转载 作者:行者123 更新时间:2023-12-02 11:32:59 25 4
gpt4 key购买 nike

我有以下结构:

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

问题:

当我尝试更新 field3 时,field4 被删除;当我尝试更新 field1 时,field2 被删除。

有人有什么建议吗?

最佳答案

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

关于java - Hibernate 使用 @CollectionTable 删除更新时具有相同 boolean 值的 set 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49150529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com