【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了BookDAO.java提供大家参考,希望对大家有所帮助!
/* $Id$
*
* Copyright (c) Jing Ge.
*/
package com.h3book.penguin.examples.dao;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.createNewSession;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.getSession;
import java.util.List;
import org.hibernate.Session;
import com.h3book.penguin.examples.domain.Book;
/**
* DAO for CRUD Book
*
* @author <a href="mailto:3710167@qq.com">Jing Ge</a>
* @version $Revision$
*/
public class BookDAO {
/**
* save a book
*
* @return the oid of the book
*/
public int save(Book book) {
Session session = getSession();
session.saveOrUpdate(book);
return book.getOid();
}
/**
* update a book.
*
* @return the oid of the book
*/
public int update(Book book) {
if (book.isTransient()) {
throw new IllegalArgumentException(
"can't update new book, try saving it.");
}
return save(book);
}
/**
* get the book by the given oid
*
* @return null if no such book
*/
public Book getByOid(int oid) {
Session session = createNewSession();
Book book = (Book) session.get(Book.class, oid);
session.close();
return book;
}
/**
* delete the book by the given oid
*/
public void deleteByOid(int oid) {
Session session = getSession();
session.delete(getByOid(oid));
}
public List<Book> findByName(String name) {
return getSession().createQuery("from Book where name like :name")
.setString("name", "%" + name + "%").list();
}
}