config.ConfigurationException: 找不到 cfg.xml 资源 [hibernate.cfg.xml]
config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
我正在构建一个从数据库创建、修改和查询表的休眠应用程序。该项目 运行 非常好,没有这个错误,当我尝试 运行 它现在我不知从哪里得到了这个错误。 'hibernate.cfg.xml' 位于 'src' 文件夹中,如下所示。我 运行 在 Netbeans Apache IDE 12.2 上使用它(不确定这是否相关)。
MainApp.java
package hiberApp;
import util.HiberUtil;
import org.hibernate.SessionFactory;
/*
@author Osama
*/
public final class MainApp {
private static final SessionFactory SESSION_FACTORY = HiberUtil.getSessionFactory(HiberUtil.Mapping.ANN);
public static void main(String[] args) {
// final DataLoad dataLoad = new DataLoad();
// dataLoad.createData(SESSION_FACTORY);
// final DataQueries dataQueries = new DataQueries();
// dataQueries.showAll(SESSION_FACTORY);
}
}
HiberUtil.java
package util;
import model.*;
import java.io.File;
import static java.lang.Math.log;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Osama
*/
public final class HiberUtil {
// static Logger log = LoggerFactory.getLogger(HiberUtil.class);
/**
* choose type of mapping definition XML or annotations
*/
public enum Mapping {
/**
* use xml defined mappings for configuration
*/
XML,
/**
* use annotation defined mappings for confuguration
*/
ANN;
}
/**
*
* @param mapping
* @return SessionFactory
*/
public static SessionFactory getSessionFactory(Mapping mapping) {
switch(mapping){
case XML:
return(getXMLSessionFactory());
case ANN:
return(getANNSessionFactory());
default:
return(getXMLSessionFactory());
}
}
public static SessionFactory getXMLSessionFactory() {
try {
File mappingDir = new File("src\xmlMaps");
Configuration config = new Configuration().configure();
config.setProperty("hibernate.show_sql", "false");
config.addDirectory(mappingDir);
SessionFactory sf = config.buildSessionFactory();
// log.trace("Session Factory created "+java.time.LocalDate.now());
return (sf);
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getANNSessionFactory() {
try {
Configuration config = new Configuration().configure();
config.setProperty("hibernate.show_sql", "false");
config.addAnnotatedClass(Coach.class)
.addAnnotatedClass(Matches.class)
.addAnnotatedClass(Person.class)
.addAnnotatedClass(Player.class)
.addAnnotatedClass(Position.class)
.addAnnotatedClass(Stadium.class)
.addAnnotatedClass(Team.class);
SessionFactory sf = config.buildSessionFactory();
return (sf);
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
}
错误:
Caused by: org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:234)
at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
at org.hibernate.cfg.Configuration.configure(Configuration.java:244)
at util.HiberUtil.getANNSessionFactory(HiberUtil.java:77)
... 2 more
提前致谢:)!
NetBeans 的全新安装解决了这个问题!
我正在构建一个从数据库创建、修改和查询表的休眠应用程序。该项目 运行 非常好,没有这个错误,当我尝试 运行 它现在我不知从哪里得到了这个错误。 'hibernate.cfg.xml' 位于 'src' 文件夹中,如下所示。我 运行 在 Netbeans Apache IDE 12.2 上使用它(不确定这是否相关)。
MainApp.java
package hiberApp;
import util.HiberUtil;
import org.hibernate.SessionFactory;
/*
@author Osama
*/
public final class MainApp {
private static final SessionFactory SESSION_FACTORY = HiberUtil.getSessionFactory(HiberUtil.Mapping.ANN);
public static void main(String[] args) {
// final DataLoad dataLoad = new DataLoad();
// dataLoad.createData(SESSION_FACTORY);
// final DataQueries dataQueries = new DataQueries();
// dataQueries.showAll(SESSION_FACTORY);
}
}
HiberUtil.java
package util;
import model.*;
import java.io.File;
import static java.lang.Math.log;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Osama
*/
public final class HiberUtil {
// static Logger log = LoggerFactory.getLogger(HiberUtil.class);
/**
* choose type of mapping definition XML or annotations
*/
public enum Mapping {
/**
* use xml defined mappings for configuration
*/
XML,
/**
* use annotation defined mappings for confuguration
*/
ANN;
}
/**
*
* @param mapping
* @return SessionFactory
*/
public static SessionFactory getSessionFactory(Mapping mapping) {
switch(mapping){
case XML:
return(getXMLSessionFactory());
case ANN:
return(getANNSessionFactory());
default:
return(getXMLSessionFactory());
}
}
public static SessionFactory getXMLSessionFactory() {
try {
File mappingDir = new File("src\xmlMaps");
Configuration config = new Configuration().configure();
config.setProperty("hibernate.show_sql", "false");
config.addDirectory(mappingDir);
SessionFactory sf = config.buildSessionFactory();
// log.trace("Session Factory created "+java.time.LocalDate.now());
return (sf);
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getANNSessionFactory() {
try {
Configuration config = new Configuration().configure();
config.setProperty("hibernate.show_sql", "false");
config.addAnnotatedClass(Coach.class)
.addAnnotatedClass(Matches.class)
.addAnnotatedClass(Person.class)
.addAnnotatedClass(Player.class)
.addAnnotatedClass(Position.class)
.addAnnotatedClass(Stadium.class)
.addAnnotatedClass(Team.class);
SessionFactory sf = config.buildSessionFactory();
return (sf);
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
}
错误:
Caused by: org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:234)
at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
at org.hibernate.cfg.Configuration.configure(Configuration.java:244)
at util.HiberUtil.getANNSessionFactory(HiberUtil.java:77)
... 2 more
提前致谢:)!
NetBeans 的全新安装解决了这个问题!