Hibernate數據源
運行環境
一
二
在hibernate
<?xml version=
<!DOCTYPE hibernate
<!
<!
<hibernate
<session
<!
<property name=
<property name=
<property name=
net
</property>
<!
<property name=
net
</property>
<property name=
com
</property>
<property name=
jdbc:microsoft:sqlserver://
</property>
<property name=
<property name=
<property name=
<property name=
<property name=
<property name=
<property name=
<!
<property name=
net
</property>
<property name=
java:comp/env/jdbc/northwind
</property>
<property name=
<!
<mapping resource=
</session
</hibernate
在此文件中
當時我在測試的時候出了一點問題
現在我終於明白了
其實
我的SessionFactory類如下
package zy
import net
import net
import net
/**
* Configures and provides access to Hibernate sessions
* current thread of execution
* pattern
*/
public class HibernateSessionFactory {
/**
* Location of hibernate
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file
* is place the config file in a Java package
* is the default Java package
* Examples: <br>
* <code>CONFIG_FILE_LOCATION =
* CONFIG_FILE_LOCATION =
*/
private static String CONFIG_FILE_LOCATION =
/** 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 net
/**
* Returns the ThreadLocal Session instance
* the <code>SessionFactory</code> if needed
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal
if (session == null) {
if (sessionFactory == null) {
try {
nfigure(CONFIG_FILE_LOCATION);
sessionFactory = cfg
}
catch (Exception e) {
System
e
}
}
session = sessionFactory
threadLocal
}
return session;
}
/**
* Close the single hibernate session instance
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal
threadLocal
if (session != null) {
session
}
}
/**
* Default constructor
*/
private HibernateSessionFactory() {
}
}
我的測試類如下
/*
* Created on
*
* TODO To change the template for this generated file go to
* Window
*/
package zy
import zy
import net
import junit
/**
* @author zhangyi
*
* TODO To change the template for this generated type comment go to
* Window
*/
public class HibernateSessionFactoryTest extends TestCase {
public static void main(String[] args) {
junit
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super
}
public void testCurrentSession() {
Session sessio
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28303.html