我尝试以编程方式配置我的EntityManager。我的供应商是OpenJPA,我编写了一个简单的控制台应用程序。这是我的代码
public static void main(String[] args) {
Map<String, String> properties = new HashMap<>();
properties.put("openjpa.ConnectionDriverName", "org.postgresql.Driver");
properties.put("openjpa.ConnectionURL", "jdbc:postgresql://localhost:5432/shop");
properties.put("openjpa.ConnectionUserName", "bob");
properties.put("openjpa.ConnectionPassword", "secret");
properties.put("openjpa.RuntimeUnenhancedClasses", "supported");
properties.put("openjpa.MetaDataFactory", "jpa(Types=de.jpa.demo.dto.Category;de.jpa.demo.dto.User;de.jpa.demo.dto.Order;de.jpa.demo.dto.Product)");
properties.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(foreignKeys=true)");
EntityManagerFactory factory = Persistence.createEntityManagerFactory("test", properties);
OpenJPAConfiguration configuration = ((EntityManagerFactoryImpl) factory).getConfiguration();
MetaDataRepository repositoryInstance = configuration.getMetaDataRepositoryInstance();
repositoryInstance.addPersistenceAware(Category.class);
repositoryInstance.addPersistenceAware(Order.class);
repositoryInstance.addPersistenceAware(Product.class);
repositoryInstance.addPersistenceAware(User.class);
manager = factory.createEntityManager();
}
但是我收到了错误
javax.persistence.PersistenceException: No persistence providers available for "test" after trying the following discovered implementations: org.apache.openjpa.persistence.PersistenceProviderImpl
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:182)
at de.jpa.demo.dto.OpenJpaUserRepositoryIT.init(OpenJpaUserRepositoryIT.java:56)
at de.jpa.demo.dto.OpenJpaUserRepositoryIT.before(OpenJpaUserRepositoryIT.java:22)
我做错了什么?
答案 0 :(得分:-1)
你必须有一个META-INF / persistence.xml文件,其中包含一个名为&#39; test&#39;的持久性单元。在它。
另外,请取悦删除openjpa.RuntimeUnenhancedClasses属性。这是一个错误的功能,如果你花时间找出另一个增强策略,你会更开心。