hibernate如何创建createalias左连接?

axr492tv  于 2021-07-05  发布在  Java
关注(0)|答案(2)|浏览(349)
@Entity
@Table(name = "BOOKTEST")
@NamedQueries({@NamedQuery(name = "Booktest.findAll", query = "SELECT b FROM Booktest b"), @NamedQuery(name = "Booktest.findById", query = "SELECT b FROM Booktest b WHERE b.id = :id"), @NamedQuery(name = "Booktest.findByBookName", query = "SELECT b FROM Booktest b WHERE b.bookName = :bookName")})
public class Booktest implements Serializable {
 @ManyToOne
    private Topictest topictestId;   //foreign key
    @JoinColumn(name = "USERTEST_ID", referencedColumnName = "ID")
    @ManyToOne
    private Usertest usertestId;  //foreign key
}

当我执行
session.createcriteria(booktest.class).createalias(“usertestid”,“usertestid”,criteria.leftïjoin).list();
sql日志仍然显示selectfrom booktest,usertest而不是selectfrom booktest left join usertest如何执行left join?
两个案子都试过了,但我还是没有“左连接”

Hibernate: select this_.ID as ID13_2_, this_.BOOK_NAME as BOOK2_13_2_, this_.TOP
ICTEST_ID as TOPICTEST4_13_2_, this_.USERTEST_ID as USERTEST3_13_2_, usertestid1
_.ID as ID14_0_, usertestid1_.TOPIC_NAME as TOPIC2_14_0_, usertest4_.ID as ID15_
1_, usertest4_.USER_NAME as USER2_15_1_ from BOOKTEST this_, TOPICTEST usertesti
d1_, USERTEST usertest4_ where this_.TOPICTEST_ID=usertestid1_.ID and this_.USER
TEST_ID=usertest4_.ID(+)
wrrgggsh

wrrgggsh1#

最后一个函数在新的hibernate版本中被去除。https://docs.jboss.org/hibernate/orm/5.0/javadocs/org/hibernate/fetchmode.html 使用fetchmode.join谢谢

ktecyv1j

ktecyv1j2#

试试这个

session.createCriteria(BookTest.class)
           .createAlias("topictestId", "usertestId")
           .setFetchMode("topictestId",FetchMode.EAGER)
           .list();

从理论上讲,这应该是可行的,如果不尝试没有别名。

相关问题