我正在使用Java SE应用程序,我想使用JPA EntityManager来管理数据库操作。从我的搜索中,我发现需要制作一个persistence.xml
文件。但我不知道我应该把它放在哪里。我的搜索结果表明它应该放在webRoot\META-INF
中,但我的项目不是J2EE项目,所以它没有webRoot或META-INF目录。这是获取实体经理的代码:
private static EntityManagerFactory factory = Persistence.createEntityManagerFactory("unitName");
protected EntityManager entityManager = entityManager = factory.createEntityManager();
但这总是导致javax.persistence.PersistenceException
异常,并将此消息返回给我:
No Persistence provider for EntityManager named unitName
我该如何解决这个问题?
感谢您的关注。
答案 0 :(得分:2)
答案 1 :(得分:1)
由于您正在使用Hibernate,因此可以看到其Tutorial Using the Java Persistence API (JPA):
在Java™SE环境中,持久性提供程序(在本例中为Hibernate)需要通过META-INF / persistence.xml资源名称的类路径查找来查找所有JPA配置文件。
将persistence.xml放到
./src/main/resources/META-INF/persistence.xml
如果你使用maven。./src/META-INF/persistence.xml
如果您不使用maven。完成后,上面的教程还会告诉您如何配置以下内容并逐步使用JPA。如果您需要任何持久性模板,请检查here。