Hibernate出现java.lang.NullPointerException异常,什么问题?

hohololo 2007-12-18 12:48:04
我使用MyEclpise进行开发,使用Hibernate将SQL Server中的表转化为类,然后写了一个Dao的类,文件如下:
import org.hibernate.Session;

import org.my.beans.MyT;
import org.my..hibernates.HibernateSessionFactory;

public class MytDao {
public void addMyT(MyT myt)
{
try
{
Session session = HibernateSessionFactory.getSession();
session.save(myt);
session.close();
}
catch (Exception e) {
System.err.println("出错了");
e.printStackTrace();
}

}
}
怎么在执行的时候,出现java.lang.NullPointerException,是怎么回事?出错的是在 Session session = HibernateSessionFactory.getSession();,哪位帮忙给看看?
会不会因为连接的是SQL Server数据库?但是我在DBExplorer中,连接正常呀!
...全文
1460 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
FredWorks 2007-12-18
  • 打赏
  • 举报
回复
你的HibernateSessionFacotry的代码贴出来看看。错误在这个类里面
hohololo 2007-12-18
  • 打赏
  • 举报
回复
To FredWorks:
配置文件错在哪里了?我怎么没看出来呀?
FredWorks 2007-12-18
  • 打赏
  • 举报
回复
to4楼:
事务并不是必须的。

to楼主:
你的错误在于你的配置文件是错的,所以无论是你的static块还是后面的rebuildSessionFactory()方法,都无法给你返回一个非空的sessionFactory实例。所以,下列代码就会返回空指针null。

public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null ¦¦ !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
}
lcmtimes 2007-12-18
  • 打赏
  • 举报
回复
需要在save前面启动一个Transaction transaction=session.beginTransaction();

在save后把它执行transaction.commit();
hohololo 2007-12-18
  • 打赏
  • 举报
回复
另外,hibernate.cfg.xml文件如下:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://localhost:1433
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="myeclipse.connection.profile">mydb</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="show_sql">true</property>
<mapping resource="edu/qchm/mybeans/MyT.hbm.xml" />

</session-factory>

</hibernate-configuration>
hohololo 2007-12-18
  • 打赏
  • 举报
回复
HibernateSessionFactory代码如下,我没有动,转化时生成的。

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
* 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 HibernateSessionFactory {

/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/edu/qchm/hibernates/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;

static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}

/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

/**
* 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();
}
}

/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}

/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}

}

58,446

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧