I am trying implement a template for my Model classes in my java/servlet project, with methods for create tables, insert, update, delete and select data. the start point for all this methods is this method protected EntityManager getEntityManager()
where I define where the sqlite database is located and other preferences.
But when this method is executed, the error javax.persistence.PersistenceException: No Persistence provider for EntityManager named default
happens. My current implementation for this method is:
public abstract class Dao<E extends Model> {
private Class<E> classe;
public Dao(Class<E> classe) {
this.classe = classe;
if(!exists()) create();
}
protected EntityManager getEntityManager() {
String url = getClass().getResource("/data.db").toString();
Map<String, String> properties = new HashMap<>();
properties.put("javax.persistence.jdbc.provider", "org.eclipse.persistence.jpa.PersistenceProvider");
properties.put("javax.persistence.jdbc.driver", "org.sqlite.JDBC");
properties.put("javax.persistence.jdbc.url", "jdbc:sqlite:"+url);
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default", properties);
return entityManagerFactory.createEntityManager();
}
...
}
anyone knows how to solve this, without have to include an external library like hibernate or similar?
The error you are facing:
is raised when you are trying creating the
EntityManagerFactory
in this line of code:Under the hood, this method will invoke the configured
PersistenceProvider
but according to the error either no one is configured or there is some type of misconfiguration.A
PersistenceProvider
is configured when defining your application JPA persistence unit. For this purpose you need to define an appropriatepersistence.xml
file (please, consider read for example this related article of Vlad Mihalcea).The content of this file will depend on the actual JPA implementation library that you are using, although it will be the same in a great extend.
For example, consider review the configuration provided for Hibernate in this gist:
For EclipseLink, as in your example, I came across this excellent example:
Please, note that in any case the persistence unit name should be the one you configured,
default
:The
persistence.xml
usually must be defined in theMETA-INF
directory, i.e., if you are using Maven standard layout, insrc/main/resources/META-INF/persistence.xml
.With this configuration in place, you can create the
EntityManagerFactory
manually with the following code:Regarding your comments, please, note that unless you are using some type of dependency injection mechanism - Spring, CDI - you will not be able to use the
@PersistenceUnit
annotation (please, see this related SO question): as stated, you need to create theEntityManagerFactory
manually as described above.One of the above mentioned repositories provides a more complete example.
You need to include the required dependencies, Hibernate or EcliseLink, in your project; I am aware that you mentioned if anybody knows how to do that without those libraries but, and I hope to be wrong, I think that the task of implementing a custom
PersistenceProvider
could be cornerstone. Please, take into account that you will need to implement, among other things, theEntityManager
itself, model all the transitions between the different persistent states of the entity, etc. Please, prefer instead a battle tested solution based on any of the two libraries mentioned.