开发者

hibernate how to create createalias left join?

开发者 https://www.devze.com 2022-12-09 22:10 出处:网络
@Entity @Table(name = \"BOOKTEST\") @NamedQueries({@NamedQuery(name = \"Booktest.findAll\", query = \"SELECT b FROM Booktest b\"), @NamedQuery(name = \"Booktest.findById\", query = \"SELECT b FROM Boo
@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(+)


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消