📄 eventmanager.java
字号:
package com.manning.hq.ch03;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.Transaction;import net.sf.hibernate.Session;import net.sf.hibernate.Hibernate;import net.sf.hibernate.cfg.Configuration;import java.util.List;/** * The EventManager serves to encapsulate the major operations * we're going to test for Chapter 3. This class isn't enterprise * ready, but it's complex enough to run through some test cases. * * @author Nick Heudecker * @author Patrick Peak */public class EventManager { private SessionFactory factory; public EventManager() throws Exception { initSessionFactory(); } /** * Initialize the SessionFactory instance. * * @throws Exception */ protected void initSessionFactory() throws Exception { factory = new Configuration().configure(). buildSessionFactory(); } /** * Save an Event instance, returning the id of the saved * instance. * * @param event * @return Long * @throws Exception */ public Long save(Event event) throws Exception { Session session = factory.openSession(); Transaction t = session.beginTransaction(); Long id = null; try { session.saveOrUpdate(event); t.commit(); id = event.getId(); } catch (Exception e) { t.rollback(); } finally { session.close(); } return id; } /** * Returns a List of Event instances matching a given name. * * @param name * @return List of Event instances, or null if nothing matched. * @throws Exception */ public List findByName(String name) throws Exception { Session session = factory.openSession(); List results = null; try { results = session.find("from Event e where e.name=?", name, Hibernate.STRING); } finally { session.close(); } return results; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -