Well, my situation is the following. I'm working on a final project that I have to deliver tomorrow with a friend. We are working together. I don't know what happened, from one moment to another I get the following error:
Exception in thread "main" javax.persistence.PersistenceException: Unable to build entity manager factory
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:81)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at com.carrito.dao.implement.singleton.EntityManagerSingleton.getInstanceEntityManager(EntityManagerSingleton.java:23)
at com.carrito.controller.interfaces.implement.PublicadorDAOImpl.<init>(PublicadorDAOImpl.java:16)
at com.carrito.servlet.controller.usuario.Prueba.main(Prueba.java:36)
Caused by: org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [jdbc:mysql://localhost:3306/carritodb?l [root on Default schema]]
at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:141)
at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:112)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:115)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:399)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:73)
... 6 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.getNameParser(InitialContext.java:505)
at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:135)
... 23 more
As you can see, I have a test class, to test if it saves as we work:
package com.carrito.servlet.controller.usuario;
import com.carrito.controller.interfaces.implement.PublicadorDAOImpl;
import com.carrito.model.Publicador;
import java.util.Date;
/**
*
* @author david
*/
public class Prueba {
public static void main(String[] args) {
Date fecha = new Date("12/09/2016");
Publicador pub = new Publicador(), pub2 = new Publicador(), pub3 = new Publicador();
// pub.setNombrePublicador("Mariano");
// pub.setApellidoPublicador("Rodríguez");
// pub.setUbicacionCarrito("Piedra blanca");
// pub.setFechaPublicador(fecha);
//
// pub2.setNombrePublicador("Raymond");
// pub2.setApellidoPublicador("Pozo");
// pub2.setUbicacionCarrito("Parte atrás");
// pub2.setFechaPublicador(fecha);
pub3.setNombrePublicador("Maria");
pub3.setApellidoPublicador("César");
// pub3.setUbicacionCarrito("Las Espinas, Espaillat");
PublicadorDAOImpl pubImp = new PublicadorDAOImpl();
pubImp.guardar(pub3);
}
}
I hope that if any of you have received this error, please help me to solve it, I'm going to cry
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Carrito_1.2PU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>carritodb</jta-data-source>
<class>com.carrito.model.Horario</class>
<class>com.carrito.model.Jornada</class>
<class>com.carrito.model.Carrito</class>
<class>com.carrito.model.Publicador</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
</properties>
</persistence-unit>
</persistence>
The
stacktrace
It is telling you that it needs configuration data in order to perform the necessary procedures. Yes, JPA can handle the configuration via
persistence.xml
but you have to give it the necessary information for it to work.You have to understand that JPA is a specification , in other words, what JPA does is establish the rules (interfaces) but it needs someone to implement those rules like
Hibernate
orEclipseLink
, that's why you specify a provider<provider />
that in your case does it with eclipselik:But where are the connection data to the DB? A file
persistence.xml
should go something like this:In my case I am using
Hibernate
as implementation.Something important to know is
transaction-type
what can beJTA
orRESOURCE_LOCAL
.RESOURCE_LOCAL
One is in charge of the administration of the
Persistence Unit
through theEntityMAnagerFactory
.JTA
The JavaEE application server, for example
Glassfish, Weblogic, WildFly, TomEE
, are the ones in charge of itPersistence Unit
and we can inject it through: