Java 使用Eben和Play 2.2.x的单向OneToMany中的例外情况
我在Play 2.2.1中使用Ebean,我试图在我的实体之间进行单向连接 不幸的是,我得到了以下例外,我知道如何避免Ebian使用“book\u book\u id”而不是“book\u id”: [PersistenceException:查询引发SQLException:未知列 “on子句”中的“t1.book\u book\u id”绑定值:[197]查询为:选择 t0.帐簿id c0,t1.交叉参考id c1,t1.帐簿t0左侧外部的accno c2 连接t1.book\u book\u id=t0.book\u id上的交叉参考t1,其中t0.book\u id=? 按t0订购。图书编号] 我的第一节课:Java 使用Eben和Play 2.2.x的单向OneToMany中的例外情况,java,playframework,playframework-2.0,ebean,Java,Playframework,Playframework 2.0,Ebean,我在Play 2.2.1中使用Ebean,我试图在我的实体之间进行单向连接 不幸的是,我得到了以下例外,我知道如何避免Ebian使用“book\u book\u id”而不是“book\u id”: [PersistenceException:查询引发SQLException:未知列 “on子句”中的“t1.book\u book\u id”绑定值:[197]查询为:选择 t0.帐簿id c0,t1.交叉参考id c1,t1.帐簿t0左侧外部的accno c2 连接t1.book\u book\
@Entity
@Table(name="book")
public class Book extends Model {
@Id
@Column(name="book_id")
public int bookId;
@OneToMany(cascade = CascadeType.ALL)
public List<Cross> crossReferences;
public static List<Book> filterByIds(List<Integer> BookIds){
if (BookIds.isEmpty()){
List<Book> books = new ArrayList<>();
return books;
}
else {
Query<Book> query = Ebean.createQuery(Book.class);
query.where(Expr.in("bookId", bookIds));
return query.findList();
}
}
}
你有理由这么做。此外,如果需要设置联接表,请使用注释
但是您似乎有一个不完整的双边关系(您使用bookID指向
Book
实体)。我可以通过插入
@JoinColumn(name="book_id", referencedColumnName = "book_id")
到
公共列表交叉引用;
提示:使用Ebean可以非常智能,因此在许多情况下,您甚至不需要注释jointable和jointColumn,只要在适用的情况下使用适当的类型,瞧:
模型/Book.java
@Entity
public class Book extends Model {
@Id
public Integer id;
public static Finder<Integer, Book> find = new Finder<>(Integer.class, Book.class);
public String name;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
public List<CrossReference> crossReferences;
public static List<Book> filterByIds(List<Integer> bookIds){
return find.where().in("id", bookIds).findList();
}
}
@Entity
public class CrossReference extends Model {
@Id
public Integer id;
public static Finder<Integer, CrossReference> find = new Finder<>(Integer.class, CrossReference.class);
@ManyToOne
public Book book;
}
@实体
公共类图书扩展模型{
@身份证
公共整数id;
公共静态查找器find=新查找器(Integer.class、Book.class);
公共字符串名称;
@OneToMany(mappedBy=“book”,cascade=CascadeType.ALL)
公共列表交叉引用;
公共静态列表筛选器字节(列表图书ID){
返回find.where().in(“id”,bookIds.findList();
}
}
模型/CrossReference.java
@Entity
public class Book extends Model {
@Id
public Integer id;
public static Finder<Integer, Book> find = new Finder<>(Integer.class, Book.class);
public String name;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
public List<CrossReference> crossReferences;
public static List<Book> filterByIds(List<Integer> bookIds){
return find.where().in("id", bookIds).findList();
}
}
@Entity
public class CrossReference extends Model {
@Id
public Integer id;
public static Finder<Integer, CrossReference> find = new Finder<>(Integer.class, CrossReference.class);
@ManyToOne
public Book book;
}
@实体
公共类交叉引用扩展模型{
@身份证
公共整数id;
公共静态查找器find=新查找器(Integer.class、CrossReference.class);
@许多酮
公共图书;
}
只是一个小例子,我有一个类,其中包含对自身的引用。基本上,它应该是一个树或层次结构。所以一个parentelement有许多子元素,而一个子元素正好有一个父元素
只要您将引用命名为类名(例如,element for element class),一切都会正常工作……但如果您将其命名为parentElement之类的其他名称,您将面临一个异常
选项1,引用名称=要引用的类名
@Entity
public class Element extends Model {
...
@ManyToOne(cascade = CascadeType.ALL)
private Element element;
@OneToMany(cascade = CascadeType.ALL)
private List<Criterion> elements = new ArrayList<>();
@实体
公共类元素扩展模型{
...
@多通(级联=级联类型.ALL)
私人要素;
@OneToMany(级联=级联类型.ALL)
私有列表元素=新的ArrayList();
选项2将mappedBy与其他命名一起使用,例如parentElement
@Entity
public class Element extends Model {
...
@ManyToOne(cascade = CascadeType.ALL)
private Element parentElement;
@OneToMany(mappedBy="parentElement", cascade = CascadeType.ALL)
private List<Criterion> elements = new ArrayList<>();
@实体
公共类元素扩展模型{
...
@多通(级联=级联类型.ALL)
私有元素父元素;
@OneToMany(mappedBy=“parentElement”,cascade=CascadeType.ALL)
私有列表元素=新的ArrayList();
我说了什么不同的话吗?:)我已经知道@JoinColumn,但忘记设置referencedColumnName。