[英]How to use JPA EntityManager in java SE applications
I'm working on a java SE application and I want to use JPA EntityManager for managing database operations. 我正在使用Java SE应用程序,并且想使用JPA EntityManager来管理数据库操作。 From my searches, I found that there is need to make a persistence.xml
file. 通过搜索,我发现需要制作一个persistence.xml
文件。 But I don't know where should I put it. 但是我不知道该放在哪里。 My search results say it should be place in the webRoot\\META-INF
, but my project it's not a J2EE project, so it has not webRoot or META-INF directories. 我的搜索结果说它应该放在webRoot\\META-INF
,但是我的项目不是J2EE项目,因此它没有webRoot或META-INF目录。 Here is my code for getting entity manager: 这是我获取实体管理器的代码:
private static EntityManagerFactory factory = Persistence.createEntityManagerFactory("unitName");
protected EntityManager entityManager = entityManager = factory.createEntityManager();
But this always causes javax.persistence.PersistenceException
exception and return me this message: 但这总是导致javax.persistence.PersistenceException
异常,并向我返回以下消息:
No Persistence provider for EntityManager named unitName
How can I solve this problem? 我怎么解决这个问题?
Thank you for your attention. 感谢您的关注。
Since you're using Hibernate, you can see its Tutorial Using the Java Persistence API (JPA) : 由于您正在使用Hibernate,因此可以查看其使用Java Persistence API(JPA)的教程 :
In Java™ SE environments the persistence provider (Hibernate in this case) is required to locate all JPA configuration files by classpath lookup of the META-INF/persistence.xml resource name. 在Java™SE环境中,需要使用持久性提供程序(在这种情况下为Hibernate)通过META-INF / persistence.xml资源名称的类路径查找来定位所有JPA配置文件。
Put the persistence.xml to 将persistence.xml放入
./src/main/resources/META-INF/persistence.xml
if you use maven. ./src/main/resources/META-INF/persistence.xml
如果使用maven)。 ./src/META-INF/persistence.xml
if you don't use maven. ./src/META-INF/persistence.xml
如果您不使用maven)。 Once done, the above tutorial also tells you how to configure the following and use JPA step by step. 完成后,上面的教程还将告诉您如何配置以下内容并逐步使用JPA。 Check here if you need any persistence template. 在此处检查是否需要任何持久性模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.