/**
* Data access object (DAO) for domain model class AppTbMenuitems.
* @see com.modules.menu.strutsMenu.impl.hibernate.AppTbMenuitems
* @author MyEclipse - Hibernate Tools
*/
public class AppTbMenuitemsDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(AppTbMenuitemsDAO.class);
//property constants
public static final String PATH = "path";
public static final String NAME = "name";
public static final String PARENT_ID = "parentId";
public static final String TEXT = "text";
public static final String TYPE = "type";
public static final String LINK = "link";
public static final String WIDTH = "width";
public static final String HEIGHT = "height";
public static final String ONCLICK = "onclick";
public static final String ONMOUSEOVER = "onmouseover";
public static final String ONMOUSEOUT = "onmouseout";
public static final String IMAGE = "image";
public static final String ALTIMAGE = "altimage";
public static final String TRAINORDER = "trainorder";
public List findByExample(AppTbMenuitems instance) {
log.debug("finding AppTbMenuitems instance by example");
try {
List results = getSession()
.createCriteria("com.modules.menu.strutsMenu.impl.hibernate.AppTbMenuitems")
.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;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding AppTbMenuitems instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from AppTbMenuitems as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByPath(Object path) {
return findByProperty(PATH, path);
}
public List findByName(Object name) {
return findByProperty(NAME, name);
}
public List findByParentId(Object parentId) {
return findByProperty(PARENT_ID, parentId);
}
public List findByText(Object text) {
return findByProperty(TEXT, text);
}
public List findByType(Object type) {
return findByProperty(TYPE, type);
}
public List findByLink(Object link) {
return findByProperty(LINK, link);
}
public List findByWidth(Object width) {
return findByProperty(WIDTH, width);
}
public List findByHeight(Object height) {
return findByProperty(HEIGHT, height);
}
public List findByOnclick(Object onclick) {
return findByProperty(ONCLICK, onclick);
}
public List findByOnmouseover(Object onmouseover) {
return findByProperty(ONMOUSEOVER, onmouseover);
}
public List findByOnmouseout(Object onmouseout) {
return findByProperty(ONMOUSEOUT, onmouseout);
}
public List findByImage(Object image) {
return findByProperty(IMAGE, image);
}
public List findByAltimage(Object altimage) {
return findByProperty(ALTIMAGE, altimage);
}
public List findByTrainorder(Object trainorder) {
return findByProperty(TRAINORDER, trainorder);
}
/**
* Data access interface for domain model
* @author MyEclipse - Hibernate Tools
*/
public interface IBaseHibernateDAO {
public Session getSession();
}
2 DAO基类
package test.hibernate;
import org.hibernate.Session;
/**
* Data access object (DAO) for domain model
* @author MyEclipse - Hibernate Tools
*/
public class BaseHibernateDAO implements IBaseHibernateDAO {
public Session getSession() {
return HibernateSessionFactory.getSession();;
}
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/dagl/hibernate/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}