JPA桌面应用程序 [英] JPA Desktop application

查看:128
本文介绍了JPA桌面应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JPA开发Java SWING应用程序,我在使用Java EE的JPA方面有丰富的经验, 但是在这种情况下,我想在运行时修改"persistence.xml"值.可以在Java EE中使用应用程序服务器上的JNDI来完成,但是在swing应用程序中我没有找到任何解决方案

I am developing a Java SWING application using JPA, I got much experience with JPA with Java EE, But in this case i want to modify 'persistence.xml' values in running time. this can be done in Java EE using JNDI on application server, but in swing application I didn't found any solution for that

注意:persistence.xml包含以下属性

note: persistence.xml contains following properties

<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/adlbprod?zeroDateTimeBehavior=convertToNull"/>
  <property name="javax.persistence.jdbc.password" value="123"/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.user" value="root"/>
  <property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>

其中继非常感谢任何人都可以提供帮助. 谢谢

its relay appreciated anybody can help this.. thanks

推荐答案

感谢所有人, 我找到了答案.如何在运行时修改属性 {步骤1移除要动态加载的属性}

Thanks for all, I found the answer. how to modify the property at run time {step 1 remove the properties want to load dynamically }

<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/adlbprod?zeroDateTimeBehavior=convertToNull"/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>

{第2步,为保留属性值创建一个地图}

{Step 2 create a Map for holds property value }

 Map pmap = new HashMap();
    pmap.put("javax.persistence.jdbc.password", "123");
    pmap.put("javax.persistence.jdbc.user", "root");
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAQueryPU",pmap);

    try {

        EntityManager em = emf.createEntityManager(pmap);
        Map<String, Object> properties = emf.getProperties();
        System.out.println("pro"+properties);


        Batch ct = new Batch();
        EntityTransaction transaction = em.getTransaction();
        transaction.begin();
        ct.setDescription("Test Batch");

        em.persist(ct);
        transaction.commit();


    } catch (Exception e) {
        e.printStackTrace();

    }

注意:-"JPAQueryPU"是持久性单元的名称

Note :- "JPAQueryPU" is name of the persistence unit

感谢R +

这篇关于JPA桌面应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆