Java tutorial
package com.lm.lic.manager.hibernate; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.LockMode; import org.springframework.context.ApplicationContext; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; /** * A data access object (DAO) providing persistence and search support for LicenseBlock entities. * Transaction control of the save(), update() and delete() operations can directly support Spring * container-managed transactions or they can be augmented to handle user-managed Spring * transactions. Each of these methods provides additional information for how to configure it for * the desired type of transaction control. * * @see com.lm.lic.manager.hibernate.anot.LicenseBlock * @author Ibrahim Mustafa */ public class LicenseBlockDAO extends HibernateDaoSupport { private static final Log log = LogFactory.getLog(LicenseBlockDAO.class); // property constants public static final String NUM_LEFT = "numLeft"; public static final String NUM_LICENSES = "numLicenses"; public static final String PRICE_PER_LICENSE = "pricePerLicense"; public static final String TOTAL_PRICE = "totalPrice"; public static final String CREATED_BY = "createdBy"; public static final String MODIFIED_BY = "modifiedBy"; protected void initDao() { // do nothing } public void save(LicenseBlock transientInstance) { log.debug("saving LicenseBlock instance"); try { getHibernateTemplate().save(transientInstance); log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } } public void delete(LicenseBlock persistentInstance) { log.debug("deleting LicenseBlock instance"); try { getHibernateTemplate().delete(persistentInstance); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public LicenseBlock findById(java.lang.Long id) { log.debug("getting LicenseBlock instance with id: " + id); try { LicenseBlock instance = (LicenseBlock) getHibernateTemplate() .get("com.lm.lic.manager.hibernate.anot.LicenseBlock", id); return instance; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } @SuppressWarnings("unchecked") public List<LicenseBlock> findByIsvId(Long isvId) { try { String queryString = "from LicenseBlock as lb where lb.isv.id= ?"; List<LicenseBlock> lbs = getHibernateTemplate().find(queryString, isvId); return lbs; } catch (RuntimeException re) { log.error("find by isvId failed", re); throw re; } } @SuppressWarnings("unchecked") public List<LicenseBlock> findByIsvIdProdId(Long isvId, Long prodId) { try { String queryString = "from LicenseBlock as lb where lb.isv.id= ? and lb.product.id=?"; List<LicenseBlock> lbs = getHibernateTemplate().find(queryString, isvId, prodId); return lbs; } catch (RuntimeException re) { log.error("find by isvId failed", re); throw re; } } @SuppressWarnings("unchecked") public List<LicenseBlock> findByExample(LicenseBlock instance) { log.debug("finding LicenseBlock instance by example"); try { List<LicenseBlock> results = getHibernateTemplate().findByExample(instance); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } } @SuppressWarnings("unchecked") public List<LicenseBlock> findByProperty(String propertyName, Object value) { log.debug("finding LicenseBlock instance with property: " + propertyName + ", value: " + value); try { String queryString = "from LicenseBlock as model where model." + propertyName + "= ?"; return getHibernateTemplate().find(queryString, value); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } public List<LicenseBlock> findByNumLeft(Object numLeft) { return findByProperty(NUM_LEFT, numLeft); } public List<LicenseBlock> findByNumLicenses(Object numLicenses) { return findByProperty(NUM_LICENSES, numLicenses); } public List<LicenseBlock> findByPricePerLicense(Object pricePerLicense) { return findByProperty(PRICE_PER_LICENSE, pricePerLicense); } public List<LicenseBlock> findByTotalPrice(Object totalPrice) { return findByProperty(TOTAL_PRICE, totalPrice); } public List<LicenseBlock> findByCreatedBy(Object createdBy) { return findByProperty(CREATED_BY, createdBy); } public List<LicenseBlock> findByModifiedBy(Object modifiedBy) { return findByProperty(MODIFIED_BY, modifiedBy); } @SuppressWarnings("unchecked") public List<LicenseBlock> findAll() { log.debug("finding all LicenseBlock instances"); try { String queryString = "from LicenseBlock"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } public LicenseBlock merge(LicenseBlock detachedInstance) { log.debug("merging LicenseBlock instance"); try { LicenseBlock result = (LicenseBlock) getHibernateTemplate().merge(detachedInstance); log.debug("merge successful"); return result; } catch (RuntimeException re) { log.error("merge failed", re); throw re; } } public void attachDirty(LicenseBlock instance) { log.debug("attaching dirty LicenseBlock instance"); try { getHibernateTemplate().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public void attachClean(LicenseBlock instance) { log.debug("attaching clean LicenseBlock instance"); try { getHibernateTemplate().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public static LicenseBlockDAO getFromApplicationContext(ApplicationContext ctx) { return (LicenseBlockDAO) ctx.getBean("LicenseBlockDAO"); } }