Java 如何从用户定义的目录中读取hibernate配置文件
我需要提供一个jar文件,该文件提供了一个API来使用Hibernate从数据库检索记录 例如,我有一个API:Java 如何从用户定义的目录中读取hibernate配置文件,java,hibernate,jdbc,toplink,Java,Hibernate,Jdbc,Toplink,我需要提供一个jar文件,该文件提供了一个API来使用Hibernate从数据库检索记录 例如,我有一个API: public List getUsers(String locationOfHibernateConfigFile) { } 我尝试使用c:\hibernate cfg.xml传递配置文件的位置和完整路径,如下所示: SessionFactory sessionFactory = new Configuration() .con
public List getUsers(String locationOfHibernateConfigFile) {
}
我尝试使用c:\hibernate cfg.xml
传递配置文件的位置和完整路径,如下所示:
SessionFactory sessionFactory = new Configuration()
.configure(C:\hibernate.cfg.xml).buildSessionFactory();
session = sessionFactory.openSession();
我收到一个错误,提示未找到c:\hibernate-cfg.xml
请给我一些实现同样目标的建议 我们试试看:
File file = new File("C:\hibernate.cfg.xml");
SessionFactory sessionFactory = new Configuration().configure(file).buildSessionFactory();
但不建议将此类型的配置保留在C:。我们试试:
File file = new File("C:\hibernate.cfg.xml");
SessionFactory sessionFactory = new Configuration().configure(file).buildSessionFactory();
SessionFactory sessionFactory = new Configuration()
.configure("hibernate.primaryKeys.cfg.xml")
.buildSessionFactory();
但不建议将此类型的配置保留在C:
SessionFactory sessionFactory = new Configuration()
.configure("hibernate.primaryKeys.cfg.xml")
.buildSessionFactory();
其中,hibernate.primaryKeys.cfg.xml
是用户定义的hibernate文件
这将起作用,但要确保类路径中有hibernate.primaryKeys.cfg.xml
其中,hibernate.primaryKeys.cfg.xml
是用户定义的hibernate文件
这将起作用,但请确保
hibernate.primaryKeys.cfg.xml
位于类路径中。您可以读取文件和属性:
只需将文件hibernate.cfg.xml放入resources文件夹
Properties properties = new Configuration().configure().getProperties();
String driver = properties.getProperty("hibernate.connection.driver_class");
String url = properties.getProperty("hibernate.connection.url");
String username = properties.getProperty("hibernate.connection.username");
String password = properties.getProperty("hibernate.connection.password");
您可以读取文件和属性: 只需将文件hibernate.cfg.xml放入resources文件夹
Properties properties = new Configuration().configure().getProperties();
String driver = properties.getProperty("hibernate.connection.driver_class");
String url = properties.getProperty("hibernate.connection.url");
String username = properties.getProperty("hibernate.connection.username");
String password = properties.getProperty("hibernate.connection.password");
@mohan您的类路径中有所有hibernate库吗?请检查一下。放在这里,所有JAR目录:字节码,jpa,可选,必选;和jar:hibernate3.jar、log4j.jar、slf4j-log4j12-1.6.2-sources.jar和slf4j-simple-1.6.2.jar。@mohan您的类路径中有所有hibernate库吗?请检查一下。放在这里,所有JAR目录:字节码,jpa,可选,必选;和jar:hibernate3.jar、log4j.jar、slf4j-log4j12-1.6.2-sources.jar和slf4j-simple-1.6.2.jar。