hibernate如何创建createalias左连接?
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个
理论上这应该可行,如果不行的话请尝试不使用别名。
Try this
In theory this should work, if not try it without the aliasing.
最后一个函数在新的 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