我需要做类似的事情:
session.getTransaction().begin();
session.createQuery("update Pack p set p.book = (select b from Book b where ...) where p.id = :id")
.setLong("id", pack.getId())
.executeUpdate();
session.getTransaction().commit();
并且有错误:
Exception occurred during event dispatching:
org.hibernate.TypeMismatchException: left and right hand sides of a binary logic operator were incompatibile [java.util.Set(BookKeeper.db.Pack.book) : BookKeeper.db.Book]
但是为什么呢? p.book
有一个Set<Book>
类型,与 (select b from Book b where ...)
相同必须返回。
最佳答案
如果你观察
p.book = (select b from Book b where ...)
选择应该返回一个唯一的值。如果没有,那么事情就会出错,因为你在右手边得到了一套,但左手边正在等待一本书。
这与 sql 语法非常相似。
关于java - hibernate :在更新中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7620115/