对于一个项目,我想让LEFT JOIN使用标准,但我不能得到我想要的东西。
我想这样做:从UserBooks中选择书籍LEFT JOIN Books,.... 但是我没有得到任何东西。
这就是我现在所得到的:
@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 + "%"));