最近刚用netbean5.5 ,下载了 hibernate 插件。
项目目录 : 缺省包
-----Main.java
---Student.hbm.xml
-----hibernamte.cfg.xml
model
-----Student.java
persistence
-----DAOImp.java
-----HibernateUtil.java
其中 如果用 hibernate插件 自动生成 HibernateUtil.java的话,代码如下
package persistence;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
/**
* @netbeans.hibernate.util
*/
public class NewHibernateUtil {
private static Log log = LogFactory.getLog(NewHibernateUtil.class);
private static SessionFactory sessionFactory;
private static SessionFactory getSessionFactory() {
try {
if (sessionFactory == null) {
Configuration configuration = new Configuration();
// load all beans
InputStream is = NewHibernateUtil.class.getResourceAsStream( "hibernateBeans.lst ");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line ;
while ((line = reader.readLine()) != null) {
configuration.addResource(line);
}
Properties properties = new Properties();
properties.load(NewHibernateUtil.class.getResourceAsStream( "hibernate.properties "));
configuration.setProperties(properties);
sessionFactory = configuration.buildSessionFactory();
}
} catch (Throwable ex) {
log.error( "Initial SessionFactory creation failed. ", ex);
throw new ExceptionInInitializerError(ex);
}
return sessionFactory;
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
if (s == null) {
s = getSessionFactory().openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
但是这样运行时 错误,应该是找不到配置文件
Caused by: java.lang.NullPointerException
at java.io.Reader. <init> (Reader.java:61)
at java.io.InputStreamReader. <init> (InputStreamReader.java:55)
at persistence.NewHibernateUtil.getSessionFactory(NewHibernateUtil.java:30)
... 3 more
如果用我自己写的 HibernateUtil.java
/*
* HibernateUtil.java
*
* Created on 2007-10-26, 19:24:48
*
* To change this template, choose Tools ¦ Templates
* and open the template in the editor.
*/
package persistence;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
*
* @author Administrator
*/
public class HibernateUtil {
public HibernateUtil() {
}
private static final SessionFactory sessionFactory;
static {
try
{
sessionFactory=new Configuration().configure().buildSessionFactory();
}
catch(HibernateException ex)
{
throw new RuntimeException( "Exception building SessionFactory "+ex.getMessage(),ex);
}
}
public static Session currentSession() throws HibernateException {
Session s=sessionFactory.openSession();
return s;
}
public static void closeSession(Session s)
{
if(s!=null)
s.close();
}
}
则可以运行,哪位能够帮我看下这是为什么?谢谢
项目目录 : 缺省包
-----Main.java
---Student.hbm.xml
-----hibernamte.cfg.xml
model
-----Student.java
persistence
-----DAOImp.java
-----HibernateUtil.java
其中 如果用 hibernate插件 自动生成 HibernateUtil.java的话,代码如下
package persistence;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
/**
* @netbeans.hibernate.util
*/
public class NewHibernateUtil {
private static Log log = LogFactory.getLog(NewHibernateUtil.class);
private static SessionFactory sessionFactory;
private static SessionFactory getSessionFactory() {
try {
if (sessionFactory == null) {
Configuration configuration = new Configuration();
// load all beans
InputStream is = NewHibernateUtil.class.getResourceAsStream( "hibernateBeans.lst ");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line ;
while ((line = reader.readLine()) != null) {
configuration.addResource(line);
}
Properties properties = new Properties();
properties.load(NewHibernateUtil.class.getResourceAsStream( "hibernate.properties "));
configuration.setProperties(properties);
sessionFactory = configuration.buildSessionFactory();
}
} catch (Throwable ex) {
log.error( "Initial SessionFactory creation failed. ", ex);
throw new ExceptionInInitializerError(ex);
}
return sessionFactory;
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
if (s == null) {
s = getSessionFactory().openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
但是这样运行时 错误,应该是找不到配置文件
Caused by: java.lang.NullPointerException
at java.io.Reader. <init> (Reader.java:61)
at java.io.InputStreamReader. <init> (InputStreamReader.java:55)
at persistence.NewHibernateUtil.getSessionFactory(NewHibernateUtil.java:30)
... 3 more
如果用我自己写的 HibernateUtil.java
/*
* HibernateUtil.java
*
* Created on 2007-10-26, 19:24:48
*
* To change this template, choose Tools ¦ Templates
* and open the template in the editor.
*/
package persistence;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
*
* @author Administrator
*/
public class HibernateUtil {
public HibernateUtil() {
}
private static final SessionFactory sessionFactory;
static {
try
{
sessionFactory=new Configuration().configure().buildSessionFactory();
}
catch(HibernateException ex)
{
throw new RuntimeException( "Exception building SessionFactory "+ex.getMessage(),ex);
}
}
public static Session currentSession() throws HibernateException {
Session s=sessionFactory.openSession();
return s;
}
public static void closeSession(Session s)
{
if(s!=null)
s.close();
}
}
则可以运行,哪位能够帮我看下这是为什么?谢谢