如您所见,我有一个 HibernateUtil 用于将数据持久保存到我的数据库中。我在 userBean.save 方法中使用的代码与我在 junit 测试中使用的代码相同。在我下面的 junit 测试中,它运行良好。但是,当我尝试在命令按钮中调用 userBean.save 方法时,它给了我一个错误,org.hibernate.HibernateException: /hibernate.cfg.xml not found。
经过一些调试,我可以告诉你错误发生在 hibernateUtil 类 configuration.configure().addAnnotatedClass(userBean.class);
如果我将命令按钮操作更改为欢迎,这是我的欢迎页面的名称,表单通过将我重定向到欢迎页面并在页面上输出 bean 值可以正常工作。这意味着,就我而言,bean 已正确初始化。这很令人困惑,为什么它在我的 junit 测试中工作而不是在我的 jsf 页面上?
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static ServiceRegistry serviceRegistry;
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure().addAnnotatedClass(userBean.class);
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
// Create the SessionFactory from hibernate.cfg.xml
return sessionFactory;
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
package Beans;
import javax.faces.bean.ManagedBean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.*;
import controller.HibernateUtil;
import javax.persistence.Table;
@ManagedBean
@Entity
@Table(name = "userBean")
public class userBean {
@Id @GeneratedValue
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String name;
private int age=0;
public String getName() {
return name;
}
public void setName(String n) {
this.name = n;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//Methods
public void save(){
//HibernateUtil.getSessionFactory().close();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
userBean u=new userBean();
u.setName("saveName");
u.setAge(20);
session.save(u);
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
}
Junittest.java
public void test2(){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
userBean u=new userBean();
u.setName("tommy");
u.setAge(20);
session.save(u);
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
索引.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>JSF 2: Blank Starting-Point Project</title>
<link href="./css/styles.css"
rel="stylesheet" type="text/css"/>
</h:head>
<h:body>
<h:form>
<h:inputText id="it" value="#{userBean.name}"/>
<h:commandButton id="btn" value="Submit" action="#{userBean.save}"/>
</h:form>
</h:body>
</html>
更多信息
正如我所说,问题来自 configuration.configure().addAnnotatedClass(userBean.class);
因此,当我们跨越这段代码时,我通过调试器并复制了控制台输出。这是工作 Junit 测试的输出
000021: Bytecode provider name : javassist
Mar 24, 2013 2:10:21 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Mar 24, 2013 2:10:21 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Mar 24, 2013 2:10:21 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Mar 24, 2013 2:10:21 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
这是来自不工作的jsf运行的代码
14:23:03,988 INFO [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-2) HHH000043: Configuring from resource: /hibernate.cfg.xml
14:23:03,998 INFO [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-2) HHH000040: Configuration resource: /hibernate.cfg.xml
14:23:04,057 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found
实际上,这是我的文件夹结构的图像,其中还显示了我的 src 和已部署的资源