这可能与我的question from a few days ago有关,但我甚至不确定如何解释这一部分。 (这是完全不同的亲子关系。)
在我的界面中,我在一对多关系中为每个属性设置了一组属性(属性)和有效值(ValidValue)。在Spring MVC前端,我有一个页面供管理员编辑这些值。提交后,如果其中任何字段(如< input>标记)为空,我会删除ValidValue对象,如下所示:
Set<ValidValue> existingValues = new HashSet<ValidValue>(attribute.getValidValues());
Set<ValidValue> finalValues = new HashSet<ValidValue>();
for(ValidValue validValue : attribute.getValidValues()) {
if(!validValue.getValue().isEmpty()) {
finalValues.add(validValue);
}
}
existingValues.removeAll(finalValues);
for(ValidValue removedValue : existingValues) {
getApplicationDataService().removeValidValue(removedValue);
}
attribute.setValidValues(finalValues);
getApplicationDataService().modifyAttribute(attribute);
问题在于,当数据库被适当更新时,下次我查询属性对象时,它们会在其ValidValue集中返回一个额外的条目 - null,因此,下次迭代时要显示的值,它在中间显示一个额外的空白值。我已经确认这发生在合并或查找时,在“执行查询ReadObjectQuery(entity.Attribute)”。
这是我用来修改数据库的代码(在ApplicationDataService中):
public void modifyAttribute(Attribute attribute) {
getJpaTemplate().merge(attribute);
}
public void removeValidValue(ValidValue removedValue) {
ValidValue merged = getJpaTemplate().merge(removedValue);
getJpaTemplate().remove(merged);
}
以下是实体类的相关部分:
Entity
@Table(name = "attribute")
public class Attribute {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "attribute")
private Set<ValidValue> validValues = new HashSet<ValidValue>(0);
}
@Entity
@Table(name = "valid_value")
public class ValidValue {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "attr_id", nullable = false)
private Attribute attribute;
}
答案 0 :(得分:0)
另一个JPA问题由几个小时的随机实验确定。希望这可以帮助其他人解决同样的问题。
问题显然是我试图删除修改后的对象。基本上,我正在做的是让Spring表单直接修改ValidValue对象,然后迭代以抛出任何被修改为值“”(空字符串)的内容。
一旦到达数据管理员,我试图分两步删除:
在这个序列中,下次我获得Attribute对象时,由于某种原因,集合中有一个额外的空值。如果我得到了所有的ValidValue对象,就没有这样的问题。
我将其更改为执行以下操作:
瞧,这解决了这个问题! (我很感激任何关于我所做的事情是完全错误的评论,并且会引发问题,但是现在,它仍有效。)