📄 hibernatesessionfac.java~32~
字号:
package util.db;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;
import strutsfinal.ProductForm;
import org.hibernate.SessionFactory;
import strutsfinal.ProductClass;
import org.hibernate.Criteria;
import org.hibernate.criterion.*;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
/**
* 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 HibernateSessionFac {
/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
System.out.println("fac build");
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* Default constructor.
*/
private HibernateSessionFac() {
}
public static void main(String[] args){
System.out.println("begin");
Session session =HibernateSessionFac.currentSession(); //HibernateUtil.currentSession();
System.out.println("session get");
Criteria crit = session.createCriteria(ProductForm.class)
.add(Expression.sql("len(productname)<5"));
List list=crit.list();
Query query=session.createSQLQuery("select * from product");
List list1=query.list();
HibernateSessionFac.closeSession();
ProductForm product;
for(int i=0;i<list.size();i++){
product=(ProductForm)list.get(i);
System.out.println(product.getProductname());
}
for(int i=0;i<list1.size();i++){
Object[] obj=(Object[])list1.get(i);
for(int j=0;j<obj.length;j++){
System.out.println(obj[j].getClass().getName());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -