使用标准API进行Hibernate搜索

使用标准API进行Hibernate搜索,hibernate,hibernate-criteria,Hibernate,Hibernate Criteria,我有两个实体类Book和chapter。Book和chapter之间有一对多映射。 我想通过提供图书名称来搜索图书和相关章节。我还想通过提供章节名称来搜索章节和相关书籍。 我可以通过提供图书名称部分实现搜索。但无法通过提供章节名称搜索章节和相关图书 我的实体类 Book.java @Entity @Table(name="book") public class Book implements Serializable{ @Id @Column(name="bookId") @Generated

我有两个实体类Book和chapter。Book和chapter之间有一对多映射。 我想通过提供图书名称来搜索图书和相关章节。我还想通过提供章节名称来搜索章节和相关书籍。 我可以通过提供图书名称部分实现搜索。但无法通过提供章节名称搜索章节和相关图书

我的实体类

Book.java

@Entity
@Table(name="book")
public class Book implements Serializable{

@Id
@Column(name="bookId")
@GeneratedValue
private Integer bookId;

@Column(name="bookName")
private String bookName;

@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinTable(
        name="BookChapter",
        joinColumns = @JoinColumn(name="BOOK_ID"),
        inverseJoinColumns = @JoinColumn(name="CHAPTER_ID")
)
private Set<Chapter> chapter;

public Integer getBookId() {
    return bookId;
}

public void setBookId(Integer bookId) {
    this.bookId = bookId;
}

public String getBookName() {
    return bookName;
}

public void setBookName(String bookName) {
    this.bookName = bookName;
}

public Set<Chapter> getChapter() {
    return chapter;
}

public void setChapter(Set<Chapter> chapter) {
    this.chapter = chapter;
}
}
服务类:-bookService.java和chapterService.java

@Service("bookService")
@Transactional
public class BookService {

@Resource(name="sessionFactory")
private SessionFactory sessionFactory;

public List<Book> getAll(){
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery("FROM Book");
    return query.list();
}

public List<Book> sortAll(){
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Book.class);
    criteria.addOrder(Order.asc("bookName"));
    return criteria.list();
}

public List<Book> Des(){
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Book.class);
    criteria.addOrder(Order.desc("bookName"));
    return criteria.list();
}

public Book get(Integer bookId){
    Session session = sessionFactory.getCurrentSession();
    return (Book)session.get(Book.class,bookId);
}

public void add(Book book) {
    Session session = sessionFactory.getCurrentSession();
    session.save(book);
}

public void delete(Integer bookId){
    Session session = sessionFactory.getCurrentSession();
    Book book = (Book)session.get(Book.class,bookId);
    session.delete(book);
}

public void edit(Book book){
    Session session = sessionFactory.getCurrentSession();
    Book book1 = (Book)session.get(Book.class,book.getBookId());

    book1.setBookName(book.getBookName());
    session.save(book1);
}

public List<Book> searchBook(String bookName){
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Book.class);
    criteria.add(Restrictions.like("bookName",bookName+"%") );
    return criteria.list();
}
}
@Service("chapterService")
@Transactional
public class ChapterService {

@Resource(name="sessionFactory")
private SessionFactory sessionFactory;

public List<Chapter> getAll(Integer bookId){
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery("FROM Book as b WHERE b.id="+bookId);
    Book book = (Book)query.uniqueResult();
    return new ArrayList<Chapter>(book.getChapter());
}

public List<Chapter>getAll(){
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery("FROM Chapter");
    return query.list();
}

public Chapter get(Integer chapterId){
    Session session = sessionFactory.getCurrentSession();
    Chapter chapter = (Chapter)session.get(Chapter.class,chapterId);
    return chapter;
}

public void add(Integer bookId,Chapter chapter){
    Session session = sessionFactory.getCurrentSession();
    session.save(chapter);

    Book book1 = (Book)session.get(Book.class,bookId);
    book1.getChapter().add(chapter);

    session.save(book1);
}

public void delete(Integer chapterId){
    Session session = sessionFactory.getCurrentSession();
    //Query query = session.createSQLQuery("DELETE FROM bookchapter"+"WHERE CHAPTER_ID="+chapterId);

    //query.executeUpdate();
    Chapter chapter = (Chapter)session.get(Chapter.class,chapterId);
    session.delete(chapter);
}

public void edit(Chapter chapter){
    Session session = sessionFactory.getCurrentSession();
    Chapter chapter1 = (Chapter)session.get(Chapter.class,chapter.getChapterId());

    chapter1.setChapterName(chapter.getChapterName());
    session.save(chapter1);
}

public List<Chapter> searchChapter(String chapterName){
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Chapter.class);
    criteria.add(Restrictions.like("chapterName",chapterName+"%") );
    return criteria.list();
}

}
bookService.java

@Service("bookService")
@Transactional
public class BookService {

@Resource(name="sessionFactory")
private SessionFactory sessionFactory;

public List<Book> getAll(){
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery("FROM Book");
    return query.list();
}

public List<Book> sortAll(){
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Book.class);
    criteria.addOrder(Order.asc("bookName"));
    return criteria.list();
}

public List<Book> Des(){
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Book.class);
    criteria.addOrder(Order.desc("bookName"));
    return criteria.list();
}

public Book get(Integer bookId){
    Session session = sessionFactory.getCurrentSession();
    return (Book)session.get(Book.class,bookId);
}

public void add(Book book) {
    Session session = sessionFactory.getCurrentSession();
    session.save(book);
}

public void delete(Integer bookId){
    Session session = sessionFactory.getCurrentSession();
    Book book = (Book)session.get(Book.class,bookId);
    session.delete(book);
}

public void edit(Book book){
    Session session = sessionFactory.getCurrentSession();
    Book book1 = (Book)session.get(Book.class,book.getBookId());

    book1.setBookName(book.getBookName());
    session.save(book1);
}

public List<Book> searchBook(String bookName){
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Book.class);
    criteria.add(Restrictions.like("bookName",bookName+"%") );
    return criteria.list();
}
}
@Service("chapterService")
@Transactional
public class ChapterService {

@Resource(name="sessionFactory")
private SessionFactory sessionFactory;

public List<Chapter> getAll(Integer bookId){
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery("FROM Book as b WHERE b.id="+bookId);
    Book book = (Book)query.uniqueResult();
    return new ArrayList<Chapter>(book.getChapter());
}

public List<Chapter>getAll(){
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery("FROM Chapter");
    return query.list();
}

public Chapter get(Integer chapterId){
    Session session = sessionFactory.getCurrentSession();
    Chapter chapter = (Chapter)session.get(Chapter.class,chapterId);
    return chapter;
}

public void add(Integer bookId,Chapter chapter){
    Session session = sessionFactory.getCurrentSession();
    session.save(chapter);

    Book book1 = (Book)session.get(Book.class,bookId);
    book1.getChapter().add(chapter);

    session.save(book1);
}

public void delete(Integer chapterId){
    Session session = sessionFactory.getCurrentSession();
    //Query query = session.createSQLQuery("DELETE FROM bookchapter"+"WHERE CHAPTER_ID="+chapterId);

    //query.executeUpdate();
    Chapter chapter = (Chapter)session.get(Chapter.class,chapterId);
    session.delete(chapter);
}

public void edit(Chapter chapter){
    Session session = sessionFactory.getCurrentSession();
    Chapter chapter1 = (Chapter)session.get(Chapter.class,chapter.getChapterId());

    chapter1.setChapterName(chapter.getChapterName());
    session.save(chapter1);
}

public List<Chapter> searchChapter(String chapterName){
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Chapter.class);
    criteria.add(Restrictions.like("chapterName",chapterName+"%") );
    return criteria.list();
}

}
MainController.java

@Controller
@RequestMapping("/chapter")
public class ChapterController {

@Resource(name="chapterService")
private ChapterService chapterService;

@RequestMapping(value="/add",method=RequestMethod.GET)
public String getAdd(@RequestParam("id")Integer bookId,Model model){

    Chapter chapter = new Chapter();

    model.addAttribute("bookId",bookId);
    model.addAttribute("chapterAttribute",chapter);

    return "addChapter";
}

@RequestMapping(value="/add", method = RequestMethod.POST)
public String postAdd(@RequestParam("id")Integer bookId,@ModelAttribute("chapterAttribute")Chapter chapter){

    chapterService.add(bookId, chapter);
    return "redirect:/record/list";
}

@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String getDelete(@RequestParam("id") Integer chapterId) {

    chapterService.delete(chapterId);
    return "redirect:/record/list";
}


@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String getEdit(@RequestParam("bid") Integer bookId,@RequestParam("cid") Integer chapterId, Model model) {

    Chapter chapter1 = chapterService.get(chapterId);

    model.addAttribute("bookId",bookId);
    model.addAttribute("chapterAttribute",chapter1);

    return "editChapter";
}

@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String postEdit(@RequestParam("id") Integer chapterId,
        @ModelAttribute("chapterAttribute") Chapter chapter) {

    chapter.setChapterId(chapterId);
    chapterService.edit(chapter);

    return "redirect:/record/list";
}

}
@Controller
@RequestMapping("/record")
public class MainController {

@Resource(name="bookService")
private BookService bookService;

@Resource(name="chapterService")
private ChapterService chapterService;

@RequestMapping(value = "/list", method = RequestMethod.GET)
public String getRecords(Model model) {

    List<Book> books = bookService.getAll();

    List<BookDTO> bookDTO = new ArrayList<BookDTO>();

    for (Book book: books) {
        BookDTO dto = new BookDTO();

        dto.setBookId(book.getBookId());
        dto.setBookName(book.getBookName());

        dto.setChapter(chapterService.getAll(book.getBookId()));

        bookDTO.add(dto);
    }

    model.addAttribute("books", bookDTO);
    return "record";
}

@RequestMapping(value="/sort",method = RequestMethod.GET)
public String sortRecords(Model model){
    List<Book>books = bookService.sortAll();
    List<BookDTO> bookDTO = new ArrayList<BookDTO>();

    for(Book book:books){
        BookDTO dto = new BookDTO();
        dto.setBookId(book.getBookId());
        dto.setBookName(book.getBookName());
        dto.setChapter(chapterService.getAll(book.getBookId()));

        bookDTO.add(dto);
    }

    model.addAttribute("books",bookDTO);
    return "record";
}

@RequestMapping(value="/dsort",method = RequestMethod.GET)
public String descRecords(Model model){
    List<Book>books = bookService.Des();
    List<BookDTO> bookDTO = new ArrayList<BookDTO>();

    for(Book book:books){
        BookDTO dto = new BookDTO();
        dto.setBookId(book.getBookId());
        dto.setBookName(book.getBookName());
        dto.setChapter(chapterService.getAll(book.getBookId()));

        bookDTO.add(dto);
    }

    model.addAttribute("books",bookDTO);
    return "record";
}

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String getAdd(Model model) {

    model.addAttribute("bookAttribute", new Book());

    return "addBook";
}


@RequestMapping(value = "/add", method = RequestMethod.POST)
public String postAdd(@ModelAttribute("bookAttribute") Book book) {

    bookService.add(book);
    return "redirect:/record/list";
}


@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String getDelete(@RequestParam("id") Integer bookId) {

    bookService.delete(bookId);
    return "redirect:/record/list";
}

@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String getEdit(@RequestParam("id") Integer bookId, Model model) {

    Book book1 = bookService.get(bookId);
    model.addAttribute("bookAttribute",book1);

    return "editBook";
}

@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String postEdit(@RequestParam("id") Integer bookId, 
                            @ModelAttribute("bookAttribute") Book book) {

    book.setBookId(bookId);
    bookService.edit(book);
    return "redirect:/record/list";
}

@RequestMapping(value="/search",method = RequestMethod.GET)
public String getSearchBook(@RequestParam("bid")String bookName,@RequestParam("cid")String chapterName,Model model){

    List<Book>books = bookService.searchBook(bookName);
    List<BookDTO> bookDTO = new ArrayList<BookDTO>();


    for(Book book:books){
            BookDTO dto = new BookDTO();
            dto.setBookId(book.getBookId());
            dto.setBookName(book.getBookName());
            dto.setChapter(chapterService.getAll(book.getBookId()));

            bookDTO.add(dto);
    }

    model.addAttribute("books",bookDTO);
    return "display";

}

}   
@控制器
@请求映射(“/record”)
公共类主控制器{
@资源(name=“bookService”)
私人图书服务;
@资源(name=“chapterService”)
私人章务章务;
@RequestMapping(value=“/list”,method=RequestMethod.GET)
公共字符串getRecords(模型){
List books=bookService.getAll();
List bookDTO=new ArrayList();
用于(书籍:书籍){
BookDTO dto=新BookDTO();
setBookId(book.getBookId());
dto.setBookName(book.getBookName());
setChapter(chapterService.getAll(book.getBookId());
bookDTO.add(dto);
}
model.addAttribute(“books”,bookDTO);
返回“记录”;
}
@RequestMapping(value=“/sort”,method=RequestMethod.GET)
公共字符串排序记录(模型){
Listbooks=bookService.sortAll();
List bookDTO=new ArrayList();
用于(书籍:书籍){
BookDTO dto=新BookDTO();
setBookId(book.getBookId());
dto.setBookName(book.getBookName());
setChapter(chapterService.getAll(book.getBookId());
bookDTO.add(dto);
}
model.addAttribute(“books”,bookDTO);
返回“记录”;
}
@RequestMapping(value=“/dsort”,method=RequestMethod.GET)
公共字符串记录(模型){
Listbooks=bookService.Des();
List bookDTO=new ArrayList();
用于(书籍:书籍){
BookDTO dto=新BookDTO();
setBookId(book.getBookId());
dto.setBookName(book.getBookName());
setChapter(chapterService.getAll(book.getBookId());
bookDTO.add(dto);
}
model.addAttribute(“books”,bookDTO);
返回“记录”;
}
@RequestMapping(value=“/add”,method=RequestMethod.GET)
公共字符串getAdd(模型){
model.addAttribute(“bookAttribute”,newbook());
返回“addBook”;
}
@RequestMapping(value=“/add”,method=RequestMethod.POST)
公共字符串postAdd(@modeldattribute(“bookAttribute”)Book Book){
bookService.add(book);
返回“重定向:/record/list”;
}
@RequestMapping(value=“/delete”,method=RequestMethod.GET)
公共字符串getDelete(@RequestParam(“id”)Integer bookId){
bookService.delete(bookId);
返回“重定向:/record/list”;
}
@RequestMapping(value=“/edit”,method=RequestMethod.GET)
公共字符串getEdit(@RequestParam(“id”)Integer bookId,Model){
bookbook1=bookService.get(bookId);
model.addAttribute(“bookAttribute”,book1);
返回“编辑本”;
}
@RequestMapping(value=“/edit”,method=RequestMethod.POST)
公共字符串postEdit(@RequestParam(“id”)整数bookId,
@模型属性(“bookAttribute”)书本(书本){
book.setBookId(bookId);
图书服务编辑(图书);
返回“重定向:/record/list”;
}
@RequestMapping(value=“/search”,method=RequestMethod.GET)
公共字符串getSearchBook(@RequestParam(“bid”)字符串bookName,@RequestParam(“cid”)字符串chapterName,Model){
Listbooks=bookService.searchBook(bookName);
List bookDTO=new ArrayList();
用于(书籍:书籍){
BookDTO dto=新BookDTO();
setBookId(book.getBookId());
dto.setBookName(book.getBookName());
setChapter(chapterService.getAll(book.getBookId());
bookDTO.add(dto);
}
model.addAttribute(“books”,bookDTO);
返回“显示”;
}
}   
我可以在mainController中搜索在Requestmapping(“/search”)下实现的书籍,但如何使用CriteriaAPI搜索章节和相应的书籍。我是Hibernate的新手

Session Session=sessionFactory.getCurrentSession();
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Book.class);

// Now join Chapter to your query root (Book)
criteria.createAlias("chapters", "chapter");

// Now restrict which Books will be returned based on the joined Chapter name
criteria.add(Restrictions.iLike("chapter.chapterName", searchString);

// There may be unwanted duplicates. If so you can do this.
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

List<Book> books = criteria.list();
条件=session.createCriteria(Book.class); //现在将章节连接到您的查询根目录(书籍) 标准。createAlias(“章节”、“章节”); //现在,根据加入的章节名称限制要返回的书籍 添加(Restrictions.iLike(“chapter.chapterName”,searchString); //可能有不需要的副本。如果是这样,您可以这样做。 标准.setResultTransformer(标准规范.独立根实体); List books=criteria.List();
如果你刚开始使用Hibernate,你可以选择使用JPA2标准API来代替。