Create JPA project without persistence.xml?

2

I created a project with Swing and JPA and it is working fine, now I want to remove the file persistence.xml and create a class to work on it. I'm researching some way to do this and found in the documentation an example of how to here . But I am not able to make it work, every time I run the project it returns the error: Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named iguanaauto .

How to solve this problem?

I'm trying like this.

public class JPAUtils {  
    private static EntityManagerFactory emf;

    /** retorna o entitymanager */
    public static EntityManager getEntityManager(){
        //emf = Persistence.createEntityManagerFactory("iguanaauto");        
        EntityManager em = getEntityManagerFactory().createEntityManager();
        return em;
    }

    private static EntityManagerFactory getEntityManagerFactory(){
        if(emf == null){
            Map<String, String> properties = new HashMap<String, String>();
            properties.put("javax.persistence.provider", "org.eclipse.persistence.jpa.PersistenceProvider");                       
            properties.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver");
            properties.put("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/iguanaauto_db?createDatabaseIfNotExist=true");
            properties.put("javax.persistence.jdbc.user", "root");
            properties.put("javax.persistence.jdbc.password", "");
            properties.put("eclipselink.ddl-generation", "create-or-extend-tables");
            properties.put("eclipselink.ddl-generation.output-mode", "database");
            properties.put("eclipselink.logging.level", "FINE");

            emf = Persistence.createEntityManagerFactory("iguanaauto", properties);            
        }
        return emf;
    }

}
    
asked by anonymous 05.09.2016 / 21:15

1 answer

2

You can not completely get rid of persistence.xml because you need to have at least the name of a declared persistence unit:

<persistence>
    <persistence-unit name="nomeDaPU">
    </persistence-unit>
</persistence>

The rest of the properties you can create the way you were doing will work.

    
06.09.2016 / 02:29