Create JPA project without persistence.xml?

Asked

Viewed 485 times

2

I created a project with Swing and JPA and is working well, now I want to remove the file persistence.xml and create a class for him to work in his place. I’m researching some way to do this and I found in the documentation an example of how to do here. But I am not able to make it work, every time I run the project 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;
    }

}

1 answer

2


You have no way to get rid of persistence.xml completely, 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 that will work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.