0

I'm creating a project with swing and JPA, it works fine, but now I want to change this project to work with JPA without persistence.xml file. I'm looking for some example but still haven't found any solution. I know that with Spring has way but with swing I don't know.

There's some way to do this, how to do ?

FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118
  • 1
    How do you expect to configure your persistence units then? – Nicolas Filotto Sep 05 '16 at 17:51
  • @NicolasFilotto I think the way is to create a Class to configure like in Spring, look at: http://stackoverflow.com/questions/30905908/jpa-without-persistence-xml – FernandoPaiva Sep 05 '16 at 17:55
  • @BillyFrost I'm creating a `persistence.xml` and set the `persistence-unit`, after this I creating a Java Class to return EntityManager, it works fine, but I want to know if does any way to create this persistence.xml programatically ? – FernandoPaiva Sep 05 '16 at 18:05
  • Nope. JPA does not include that as a valid use case, and this question is nothing to do with Swing ... Swing will use data retrieved by JPA but has nothing to do with JPA. Suggest you untag Swing –  Sep 05 '16 at 18:13

1 Answers1

0

I solved the problem. The JPA always need a persistence.xml but I can create a simple persistence.xml setting only one persistence-unit and after I can create a class to define values of connection and others.

here the persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="default">    
    <exclude-unlisted-classes>false</exclude-unlisted-classes>    
  </persistence-unit>
</persistence>

and here the class where I setting the values

import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceUnitTransactionType;
import org.eclipse.persistence.jpa.PersistenceProvider;
import org.eclipse.persistence.config.PersistenceUnitProperties;

/**
 *
 * @author fernando
 */
public class JPAUtils {  
    private static EntityManagerFactory emf;
    private static final String PERSISTENCE_UNIT = "default"; 
    private static final String DB_NAME = "iguanaauto_db";
    public static String IP_SERVER = "127.000.000.001";

    /** return an entitymanagerfactory */
    public static EntityManagerFactory getEntityManagerFactory(){        
        if(emf == null){
            PersistenceProvider pp = new PersistenceProvider();
            Map<String, String> properties = new HashMap<String, String>();            
            properties.put(PersistenceUnitProperties.TRANSACTION_TYPE, PersistenceUnitTransactionType.RESOURCE_LOCAL.name());                      
            properties.put(PersistenceUnitProperties.JDBC_DRIVER, "com.mysql.jdbc.Driver");
            properties.put(PersistenceUnitProperties.JDBC_URL, "jdbc:mysql://" + IP_SERVER + ":3306/" + DB_NAME + "?createDatabaseIfNotExist=true");
            properties.put(PersistenceUnitProperties.JDBC_USER, "root");
            properties.put(PersistenceUnitProperties.JDBC_PASSWORD, "");
            properties.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_OR_EXTEND);
            properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_DATABASE_GENERATION);
            properties.put(PersistenceUnitProperties.LOGGING_LEVEL, "FINE");            
            emf = pp.createEntityManagerFactory(PERSISTENCE_UNIT, properties);
        }
        return emf;
    }

}
FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118