我在Jboss AS 7.1.1上部署了一个EAR文件。
在其中一个EJB项目中,我们在META-INF文件夹下有一个persistence.xml文件。我需要在数据库中进行批量更新,所以我编写了这个代码来获取会话工厂:
private SessionFactory buildSessionFactory() throws Exception {
SessionFactory factory = null;
try{
Configuration configuration = new Configuration();
configuration.addResource("/META-INF/persistence.xml");
configuration.configure().setProperty("hibernate.show_sql", "false"); ---> **throws error**
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
factory = configuration.buildSessionFactory(serviceRegistry);
}
catch (Throwable t){
throw new Exception(t);
}
return factory;
}
我遇到的问题是:org.hibernate.HibernateException: /hibernate.cfg.xml not found
。
我如何告诉hibernate使用persistence.xml
文件而不是hibernate.cfg.xml
?
更新
我发现我使用了错误的方法:
configuration.addResource("/META-INF/persistence.xml");
应该是:
configuration.configure("/META-INF/persistence.xml");
但是现在我有一个不同的问题,因为Jboss persistence.xml与hibernate.cfg.xml没有相同的结构,所以我得到了无效的配置异常。
知道在这里采取什么样的正确方法?
答案 0 :(得分:0)
你可以通过JPA使用Hibernate,但你必须重写一些代码 - 将SessionFactory更改为EntityManager等。其他方法是编写一个hibernate.cfg.xml配置,并使用本机Hibernate而不触及你的代码。