正如您所看到的,我有一个HibernateUtil,用于将数据保存到我的数据库中.我在userBean.save方法中使用的代码与我在junit测试中使用的代码相同.在我下面的junit测试中它完美无缺.但是,当我尝试在我的命令按钮中调用userBean.save方法时,它给出了一个错误,org.hibernate.HibernateException:/hibernate.cfg.xml未找到.
经过一些调试后,我可以告诉你hibernateUtil类中发生的错误,configuration.configure().addAnnotatedClass(userBean.class);
如果我将commandbutton操作更改为welcome(这是我的欢迎页面的名称),表单可以正常工作,将我重定向到欢迎页面并在页面上输出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();
}
的index.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和已部署的资源
最佳答案 问题的原因是JUnit找不到hibernate.cfg.xml.发生这种情况,因为您将所有* .xml资源放入WEB-INF / classes文件夹中.在这种情况下,这些资源在Web应用程序的类路径中可见,但对于JUnit测试不可见.
作为解决方案,我建议您按如下方式进行:
>将所有* .xml资源从WEB-INF / classes移动到sources文件夹的根目录.
>确保编译器不忽略* .xml文件
作为最终结果,您的* .xml资源无论如何都将位于WEB-INF / classes文件夹中,但您也可以从普通的类路径中获取这些资源.
请注意,您需要移动文件,否则可能会有版本的混乱.