java-OpenJPA在没有persistence.xml的代码中使用属性创建EntityManager
作者:互联网
我需要在没有persistence.xml的代码中创建EntityManager …
我有
props.put("openjpa.ConnectionURL", "jdbc:sqlserver://databasehost:3306; DatabaseName=dbname; selectMethod=cursor;create=true");
props.put("openjpa.ConnectionDriverName","com.mysql.jdbc.Driver");
props.put("openjpa.ConnectionUserName", "dbname");
props.put("openjpa.ConnectionPassword", "password");
props.put("openjpa.jdbc.SynchronizeMappings", "buildSchema");
props.put("openjpa.Log", "DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE");
props.put("openjpa.RuntimeUnenhancedClasses", "supported");
EntityManagerFactory factory = Persistence.createEntityManagerFactory("wp");
em = factory.createEntityManager(props);
..and当em = factory.createEntityManager(props)被调用时,我得到:
javax.persistence.PersistenceException: No persistence providers available for "wp" after trying the following discovered implementations: org.apache.openjpa.persistence.PersistenceProviderImpl at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:182) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:72) at dao.wp.WP_EMH.getCurrentEntityManager(WP_EMH.java:27)
我了解没有提及以tag表示的persistence.xml中的某些内容
PROVIDER- org.apache.openjpa.persistence.PersistenceProviderImpl-/ PROVIDER
我应该如何将其添加到属性?或如何解决?
我的第二个问题是,在persistence.xml中添加了实体类.如何用属性解决这个问题?
谢谢
解决方法:
方法
javax.persistence.Persistence.createEntityManagerFactory(
String persistenceUnitName);
需要来自persistence.xml的配置.尝试提供您的物业地图给
javax.persistence.Persistence.createEntityManagerFactory(
String persistenceUnitName, Map properties);
显然,属性集是不完整的.它缺少例如持久性类名:
properties.put("openjpa.MetaDataFactory", "jpa(Types=FQN.class1;FQN.class2;...)");
标签:openjpa,persistence,entitymanager,java 来源: https://codeday.me/bug/20191207/2087084.html