OpenJPA 在代码中创建 EntityManager,无需 persistence.xml,具有属性

发布于 2024-12-07 16:31:29 字数 1461 浏览 0 评论 0原文

我需要在没有 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);

..并且当 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)

我知道没有提到由标记

PROVIDER- org.apache.openjpa.persistence.PersistenceProviderImpl - / 表示的 persistence.xml 中的内容PROVIDER

我应该如何将其添加到属性中?或者如何解决? 我的第二个问题是在 persistence.xml 中添加了实体类。如何用属性来解决这个问题?

谢谢

I need to create EntityManager in code without persistence.xml ...
I have



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 when em = factory.createEntityManager(props) is called I get :


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)

I understand that there is no mention about something that is in persistence.xml represented by tag

PROVIDER- org.apache.openjpa.persistence.PersistenceProviderImpl - /PROVIDER

How should I add this to Properties? Or how to solve it?
And my second question is that in persistence.xml are added classes of entities. How to solve this with properties?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-12-14 16:31:29

该方法

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;...)");

The method

javax.persistence.Persistence.createEntityManagerFactory(
    String persistenceUnitName);

expects configuration from persistence.xml. Try supplying your property map to

javax.persistence.Persistence.createEntityManagerFactory(
    String persistenceUnitName, Map properties);

Clearly, though, the property set is incomplete; it lacks, for example, persistent class names:

properties.put("openjpa.MetaDataFactory", "jpa(Types=FQN.class1;FQN.class2;...)");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文