我正在使用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
答案 0 :(得分:3)
这是一个检测到的问题: https://github.com/ebean-orm/avaje-ebeanorm/issues/445
需要指定一个唯一的连接名称,我在你的Foo类中尝试了这个更改并且它有效:
@Entity
public class Foo extends BaseModel {
public static final Find<Long, Foo> find = new Find<Long, Foo>() {};
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "bars1")
private List<Bar> bars1;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "bars2")
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;
}
}
希望它有所帮助。
答案 1 :(得分:1)
(我无法添加评论,所以请原谅我添加一些可能值得考虑作为问题的完整答案的内容)
据我所知(在Hibernate中,也可能在Ebean中),默认的@Entity
关系模型假设(如果没有指定)关系的某些连接表名称。该名称是关系所有者和中间_
的串联。也许在这种情况下,无论不同的类文件名,ORM管理器,在创建提到的默认表名时,都为两个集合创建了相同的表名。换句话说:也许“在下面” - 两个字段都引用相同的连接表,并以这种方式引用到同一个集合。
(这是更多的猜测因为我现在无法测试提到的ORM行为......)