`
Cash
  • 浏览: 181179 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

Eclipse3M7+Hibernate2 运行环境测试

阅读更多
package springWebwork2;

import net.sf.hibernate.*;
import org.apache.commons.logging.*;
import java.util.*;
import java.io.*;
import net.sf.hibernate.cfg.Configuration;

import showHelper.block.*;

public class HibernateUtil {

private static Log log = LogFactory.getLog(HibernateUtil.class);

private static final SessionFactory sessionFactory;
 
static {
  try {
   // Create the SessionFactory
   Configuration conf= new Configuration().configure(new File("hibernate.cfg.xml"));
   sessionFactory = conf.buildSessionFactory();
  } catch (Throwable ex) {
   log.error("创建SessionFactory失败:", ex); 
   throw new ExceptionInInitializerError(ex);
  }
}

//使用ThreadLocal实现线程安全
public static final ThreadLocal LocalSession = new ThreadLocal();

public static Session currentSession() throws HibernateException {
  Session s = (Session) LocalSession.get();
  // Open a new Session, if this Thread has none yet
  if (s == null) {
   s = sessionFactory.openSession();
   LocalSession.set(s);
  }
  return s;
}

public static void closeSession() throws HibernateException {
  Session s = (Session) LocalSession.get();
  LocalSession.set(null);
  if (s != null)
   s.close();
}
//测试代码
public static void main(String[] args) throws Exception {
  Session session = HibernateUtil.currentSession();
  Transaction tx = session.beginTransaction();
  //创建数据对象并插入表
  Block B = new Block();
  //B.setBlockname("浑南新区");
  //B.setShoworder(0);
  //相当于insert
  //session.save(B);

  //用list方法查询数据
  String Sql="from Block as block1 order by block1.Showorder";
  Query q = session.createQuery(Sql); //用对象类名,而非表名
  List l = q.list();//返回一个List接口,用来遍历结果集

  for (int i = 0; i < l.size(); i++) {
   B = (Block) l.get(i);  
   System.out.println(B.getId()+" "+B.getBlockname());  }
  //更新数据
  //Query qq = session.createQuery("from SimpleUser");
  //B = (Block) session.load(Block.class, new Integer(1));
  //B.setBlockname("其它");
  //tx.commit();
  //关闭
  HibernateUtil.closeSession();
}
}
----------------------------------------------------------------------------------
配置文件 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">


<hibernate-configuration>
<session-factory>
<!-- local connection properties -->
<property name="hibernate.connection.url">
jdbc:postgresql://192.168.1.3/datebasename </property>
<property name="hibernate.connection.driver_class">
org.postgresql.Driver
</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">123456</property>
<!-- property name="hibernate.connection.pool_size"></property -->
<!-- dialect for PostgreSQL -->
<property name="dialect">
net.sf.hibernate.dialect.PostgreSQLDialect
</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
<mapping resource="showHelper/Block.hbm" />
</session-factory>
</hibernate-configuration>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics