@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怎么做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(+)

最佳答案

尝试这个

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


从理论上讲,这应该可以工作,如果没有混叠也可以尝试。

10-06 03:34