Persistenceunit: fabricaweb2 Unable to build Entitymanagerfactory

Asked

Viewed 809 times

0

I have this error below, you are not able to create the Entitymanager. I am using the Maven and the folder META-INF this one inside src/main/Resources in the root folder where the entities are src/main/java.

Errors below when running:

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: fabricaweb2] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:924)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:59)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at fabricaweb2.TestHibernate.main(TestHibernate.java:13)
Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
    at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:98)
    at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:68)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:170)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:76)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:132)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
    ... 5 more

Follow the persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">

<persistence-unit name="fabricaweb2" transaction-type="RESOURCE_LOCAL">
    <!-- provedor/implementacao do JPA -->
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <properties>
        <!-- dados da conexao -->
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/fabricaweb2db"/>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="root"/>
        <!-- atualiza o banco, gera as tabelas se for preciso -->
        <property name="hibernate.hbm2ddl.auto" value="update"/>

    </properties> 
</persistence-unit>

Follow the test class

package fabricaweb2;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import br.com.fabricaprogramador.entidades.Usuario;

public class TestHibernate {

    public static void main(String[] args) {
        // Fabrica de EntityManagerFactory
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("fabricaweb2");
        // Gerenciador de entidades
        EntityManager em = emf.createEntityManager();

        // Cria o Objeto Usuario
        Usuario usuario = new Usuario();
        usuario.setNome("Pedro"); 
        usuario.setLogin("p");
        usuario.setSenha("234"); 
        // Inicando a Transação
        em.getTransaction().begin(); 
        // Prepara a instrução SQL 
        em.persist(usuario);
        // Confirmando a transação, fazendo a persistencia no banco 
        em.getTransaction().commit();

    }
}

Follow the pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.fabricaprogramador</groupId>
<artifactId>fabricaprogramador</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<!-- Dependdecias do Hibernate -->

<dependencies>

    <!-- Núcleo do Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.2.6.Final</version>
        <scope>compile</scope>
    </dependency>
    <!-- Implementação de EntityManager da JPA -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.6.Final</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId> org.hibernate </groupId>
        <artifactId> hibernate-validator </artifactId>
        <version> 4.3.0.Final </version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.2.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency> 

    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <version>1.1.1.Final</version>
        <scope>provided</scope>
    </dependency>

    <!-- Driver jdbc do postgresql -->
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.4</version>
    </dependency>

</dependencies>

<build>
    <finalName>fabricaweb2</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Follow the new error:

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1397)
    at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:62)
    at fabricaweb2.TestHibernate.main(TestHibernate.java:23)
Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:221)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
    at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1426)
    at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:59)
    ... 1 more
Caused by: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "root"
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:293)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:108)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:32)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:393)
    at org.postgresql.Driver.connect(Driver.java:267)
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:204)
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
    ... 6 more
  • Are you sure the postgres are at gate 80, not at standard 5432 or any other? Have you tried jdbc:postgresql://localhost:5432/fabricaweb2db in place of jdbc:postgresql://localhost/fabricaweb2db ?

  • Alias, now this error 'No Persistence Provider for Entitymanager named fabricaweb2db'

  • Remove this line '<Provider>org.hibernate.ejb.Hibernatepersistence</Provider>' in a know if it was this tag q Voce falo, but the error remains the same ..

  • After making a few attempts here it is now the bank that gave problem. ERROR: 'Could not open Connection '

  • I’ll try to reproduce here

  • Okye Bruno on hold. I’m suspicious of the postgre version, in pom.xml

  • In this case, only including the dialect explicitly worked. Include the stack trace complete of Could not open Connection

  • Okey Bruno I will edit the question and insert the full stack trace, because here it does not fit :D

  • FATAL: password authentication failed for user "root": this is the error now, so there is some problem with your user/password

  • vdd. I don’t understand why I’ve already put and password 'root' and user 'root' and this error tbem appears, I’m in the cause of this error..

  • After I added the slf4j statements and made some changes to the project, the error is only in the root password and root user' .

Show 6 more comments

1 answer

1

Include the postgres dialect in the persistence.xml file as below:

<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>

  • Thanks put this property, helped me but now surgui another error: 'Could not open Connection '

  • About the new error, the password is incorrect. You have already used the Postgres database client (pgAdminIII) to validate credentials and connect to the database?

  • Yes, the postgre setting is in persistence.xml

Browser other questions tagged

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