Search code examples
swingjpajndi

JPA Desktop application


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


Solution

  • 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+