📄 booktabledao.java
字号:
package com.xfaccp.bookonline.hib.dao;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import com.xfaccp.bookonline.hib.dao.base.BaseHibernateDAO;
import com.xfaccp.bookonline.hib.dto.BookTable;
import com.xfaccp.bookonline.hib.dto.CatalogTable;
import com.xfaccp.bookonline.hib.dto.base.IBaseHibernateDTO;
/**
* Data access object (DAO) for domain model class BookTable.
*
* @see com.xfaccp.bookonline.hib.dto.BookTable
* @author MyEclipse - Hibernate Tools
*/
public class BookTableDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(BookTableDAO.class);
// property constants
public static final String BOOKNAME = "bookname";
public static final String PRICE = "price";
public static final String PICTURE = "picture";
public boolean save(IBaseHibernateDTO transientInstance) {
Session session = this.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
BookTable bookTable = (BookTable) transientInstance;
CatalogTable catalogTable = (CatalogTable) session.load(
CatalogTable.class, bookTable.getCatalogid());
bookTable.setCatalogTable(catalogTable);
Serializable se = session.save(bookTable);
tx.commit();
if (se != null) {
return true;
}
} catch (Exception re) {
if (tx != null)
tx.rollback();
re.printStackTrace();
return false;
} finally {
session.close();
}
return false;
}
public BookTable findById(java.lang.Integer id) {
log.debug("getting BookTable instance with id: " + id);
Session session = this.getSession();
try {
BookTable instance = (BookTable) session.get(
"com.xfaccp.bookonline.hib.dto.BookTable", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
} finally {
}
}
public List findByExample(BookTable instance) {
log.debug("finding BookTable instance by example");
Session session = this.getSession();
try {
List results = session.createCriteria(
"com.xfaccp.bookonline.hib.dto.BookTable").add(
Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
} finally {
// session.close();
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding BookTable instance with property: " + propertyName
+ ", value: " + value);
Session session = this.getSession();
try {
String queryString = "from BookTable as model where model."
+ propertyName + "= ?";
Query queryObject = session.createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
} finally {
session.close();
}
}
public List findByBookname(Object bookname) {
return findByProperty(BOOKNAME, bookname);
}
public List findByPrice(Object price) {
return findByProperty(PRICE, price);
}
public List findByPicture(Object picture) {
return findByProperty(PICTURE, picture);
}
public BookTable merge(BookTable detachedInstance) {
log.debug("merging BookTable instance");
Session session = this.getSession();
try {
BookTable result = (BookTable) session.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
} finally {
session.close();
}
}
public void attachDirty(BookTable instance) {
log.debug("attaching dirty BookTable instance");
Session session = this.getSession();
try {
session.saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
} finally {
session.close();
}
}
public void attachClean(BookTable instance) {
log.debug("attaching clean BookTable instance");
Session session = this.getSession();
try {
session.lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
} finally {
session.close();
}
}
public void updateMethod(BookTable bookTable) {
Session session = this.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
BookTable book = this.findById(bookTable.getBookid());
CatalogTable catalog = new CatalogTableDAO().findById(bookTable
.getCatalogid());
book.setBookname(bookTable.getBookname());
book.setCatalogTable(catalog);
book.setPrice(bookTable.getPrice());
book.setPicture(bookTable.getPicture());
session.update(book);
tx.commit();
} catch (Exception ex) {
if (tx != null) {
tx.rollback();
}
ex.printStackTrace();
}finally{
session.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -