Error using JPA

0

Well, my situation is as follows. I am working on a final project that I have to deliver tomorrow with a friend. We are working at par. I do not know what happened, from one moment to the next I get the 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 keeps 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 has given this error, help me solve it please, 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>
    
asked by Brian Martínez 11.12.2016 в 14:32
source

1 answer

2

stacktrace

  

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

It is telling you that you need configuration data to be able to perform the necessary procedures. Yes, JPA can manage the configuration through persistence.xml but you have to give it the necessary information so that it can work.

It is necessary to understand that JPA is a specification , in other words, JPA what it does is to establish the rules (interfaces) but it needs someone to implement those rules as Hibernate or EclipseLink , that is why you specify a provider <provider /> that in your case does it with eclipselik:

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

But where are the DB connection data? a file persistence.xml should go something like this:

<?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="Tienda_V4.2PU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>    
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>           
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/nombre_base_de_datos"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>        
            <property name="javax.persistence.schema-generation.database.action" value="create"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        </properties>
    </persistence-unit>
</persistence>

In my case, I'm using Hibernate as implementation.

Something important to know is the transaction-type that can be JTA or RESOURCE_LOCAL .

RESOURCE_LOCAL

One is in charge of the administration of Persistence Unit through the EntityMAnagerFactory .

JTA

The JavaEE application server such as Glassfish, Weblogic, WildFly, TomEE is responsible for Persistence Unit and we can do it inject through:

@persistenceContext(unitNAme="nombre_persiste_unit")   
private EntityManager em;
    
answered by 11.12.2016 / 16:29
source