hibernate如何创建createalias左连接?

发布于 2024-08-09 23:10:35 字数 1401 浏览 3 评论 0原文

@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_log 仍然显示 select * from booktest,usertest 而不是 select * from booktest left join usertest 如何进行左连接?

尝试了非连续的两种情况,我仍然低于没有“左连接”

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(+)
@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
}

when i execute

session.createCriteria(Booktest.class).createAlias("usertestId", "usertestId",Criteria.LEFT_JOIN)
.list();

the sql_log still showing select * from booktest,usertest rather than select * from booktest left join usertest how to do left join?

tried non sequitor both cases and i still getting below without "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(+)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热情消退 2024-08-16 23:10:35

试试这个

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

理论上这应该可行,如果不行的话请尝试不使用别名。

Try this

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

In theory this should work, if not try it without the aliasing.

君勿笑 2024-08-16 23:10:35

最后一个函数在新的 hibernate 版本中已被废弃。
https://docs.jboss.org/hibernate/orm /5.0/javadocs/org/hibernate/FetchMode.html
使用 FetchMode.JOIN
谢谢

This last function is depricated in new hibernate version.
https://docs.jboss.org/hibernate/orm/5.0/javadocs/org/hibernate/FetchMode.html
use FetchMode.JOIN
thank you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文