2016-04-07 83 views
0

對於一個項目,我想獲得一個LEFT JOIN與標準的工作,但我不「噸得到我想要的東西Hibernate的標準LEFT JOIN

我想這樣做:從UserBooks選擇左側的書籍JOIN書籍,.. .. 但我沒有得到任何

這是我的時刻了:

@SuppressWarnings("unchecked") 
public List<Books> getBooksByTitle(int userID, String Title) { 
    List<Books> books = null; 

    Configuration cfg = new Configuration(); 
    cfg.configure("hibernate.cfg.xml"); 
    SessionFactory factory = cfg.buildSessionFactory(); 
    Session session = factory.openSession(); 
    Transaction tx = session.beginTransaction(); 

    Criteria c = session.createCriteria(UserBooks.class, "userBooks"); 
    c.setFetchMode("userBooks.book", FetchMode.JOIN); 
    c.createAlias("userBooks.book", "books"); 
    c.setProjection(Projections.property("books")) ; 
    c.add(Restrictions.eq("userBooks.id", userID)); 
    c.add(Restrictions.like("books.title", "%"+ Title + "%")); 

    books = c.list(); 

    for (Books allBooks : (List<Books>)books) { 
       System.out.println(">>>>>" + allBooks.getTitle()); 
    } 

    tx.rollback(); 
    session.close(); 
    factory.close(); 
    return books; 
} 
+0

你想加入什麼桌子? –

回答

0

別介意我得到它:)

Criteria c = session.createCriteria(UserBooks.class, "userBooks"); 
    c.createAlias("userBooks.book", "book"); 
    c.setProjection(Projections.property("book")); 
    c.add(Restrictions.eq("userBooks.user.id", userID)); 
    c.add(Restrictions.like("book.title", "%" + Title + "%"));