【发布时间】:2016-08-31 09:14:16
【问题描述】:
我正在使用 Ninja 框架,我正在尝试创建一个表,其中包含两个同类其他表的列表。问题是另一个列表中的所有内容也在另一个列表中。
主要:
test();
List<Foo> foos = Foo.find.all();
for(Foo foo : foos){
System.out.println("Printing bars1, size: " + foo.getBars1().size());
for(Bar bar : foo.getBars1()){
System.out.println(bar.getText());
}
System.out.println("Printing bars2, size: " + foo.getBars2().size());
for(Bar bar : foo.getBars2()){
System.out.println(bar.getText());
}
}
功能测试:
private void test() {
Foo foo = new Foo();
Bar bar1 = new Bar();
Bar bar2 = new Bar();
bar1.setText("This should only be in bars1");
bar2.setText("This should only be in bars2");
foo.getBars1().add(bar1);
foo.getBars2().add(bar2);
foo.save();
}
富:
package models;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
@Entity
public class Foo extends BaseModel {
public static final Find<Long, Foo> find = new Find<Long, Foo>() {};
@OneToMany(cascade = CascadeType.ALL)
private List<Bar> bars1;
@OneToMany(cascade = CascadeType.ALL)
private List<Bar> bars2;
public List<Bar> getBars1() {
return bars1;
}
public void setBars1(List<Bar> bars1) {
this.bars1 = bars1;
}
public List<Bar> getBars2() {
return bars2;
}
public void setBars2(List<Bar> bars2) {
this.bars2 = bars2;
}
}
酒吧:
package models;
import javax.persistence.Entity;
import javax.validation.constraints.Size;
@Entity
public class Bar extends BaseModel {
public static final Find<Long, Bar> find = new Find<Long, Bar>() {};
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
从主打印:
Printing bars1, size: 2
This should only be in bars1
This should only be in bars2
Printing bars2, size: 2
This should only be in bars1
This should only be in bars2
预期:
Printing bars1, size: 1
This should only be in bars1
Printing bars2, size: 1
This should only be in bars2
【问题讨论】:
-
您是否尝试过将
CascadeType更改为不同的值?在我看来,输入CascadeType.ALL或完全删除它并查看行为是否保持不变是个坏主意。如果不知道,问题的根源是将所有持久性操作从Bar一直传播到Foo,这可能导致覆盖表(将两个列表合并为一个表)
标签: java arraylist orm ebean ninjaframework