JPA Desktop application

半世苍凉 提交于 2019-12-08 02:37:56

问题


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

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>

{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();

    }

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

Thanks R+



来源:https://stackoverflow.com/questions/14398108/jpa-desktop-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!