0

Hibernate を使用してデータベースからレコードを取得するための API を提供する jar ファイルを提供する必要があります。

たとえば、私はAPIを持っています:

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 is not found

同じことを達成するためのいくつかの指針を教えてください。

4

3 に答える 3

2
SessionFactory sessionFactory = new Configuration()
    .configure("hibernate.primaryKeys.cfg.xml")
    .buildSessionFactory();

hibernate.primaryKeys.cfg.xmlユーザー定義の休止状態ファイルはどこにありますか。

これは機能hibernate.primaryKeys.cfg.xmlしますが、クラスパスにあることを確認してください。

于 2013-12-07T03:46:58.017 に答える
1

試してみてください:

File file = new File("C:\hibernate.cfg.xml");
SessionFactory sessionFactory = new Configuration().configure(file).buildSessionFactory();

ただし、このタイプの構成を C: に残すことはお勧めできません。

于 2011-12-12T17:52:12.920 に答える
1

ファイルとプロパティを読み取ることができます:

ファイル 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");
于 2020-09-15T19:03:27.963 に答える