11
respostas

javax.persistence.PersistenceException

ola ja verifiquei todo o meu código e mesmo assim continua dando erro.

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named financas at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) at br.com.laisa.finacas.Teste.TestaConta.main(TestaConta.java:19)

11 respostas

Olá Laisa!

Pode me mandar seu código pra eu dar uma olhada por favor? Assim fica mais fácil te ajudar no problema! ^^

Aguardo sua resposta! :D

Consegui compilar e gerou no banco de dados, era o provider que estava errado e também deu erro de timezone. Agora esta dando esse erro , favor ajudar porque a gente acaba perdendo muito tempo nessas coisas , e deveria ser melhor explicado no curso essas atualizações das bibliotecas que e o que parece que ta dando errado. Baixei o connector 8.0 do mysql porque esse que vem no projeto nao roda o codigo , mas ainda da esse erro.

INFO: HHH000412: Hibernate Core {5.4.1.Final}
fev 01, 2019 10:53:19 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
fev 01, 2019 10:53:20 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
fev 01, 2019 10:53:20 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/financas]
fev 01, 2019 10:53:20 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
fev 01, 2019 10:53:20 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
fev 01, 2019 10:53:20 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
fev 01, 2019 10:53:20 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
fev 01, 2019 10:53:22 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@3cc20577] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
fev 01, 2019 10:53:22 AM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: br.com.laisa.finacas.modelo.Conta
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
    at br.com.laisa.finacas.Teste.TestaConta.main(TestaConta.java:23)
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: br.com.laisa.finacas.modelo.Conta
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:127)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:62)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:804)
    ... 2 more
Os meu codigos:

package br.com.laisa.financas.Teste;

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

import br.com.laisa.financas.modelo.Conta;

public class TestaConta {

public static void main(String[] args) {

    Conta conta = new Conta();
    conta.setAgencia("123");
    conta.setBanco("Brasil");
    conta.setId(14);
    conta.setTitular("laisa");

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("financas");
    EntityManager em = emf.createEntityManager();

    em.getTransaction().begin();
    em.persist(conta);
    em.getTransaction().commit();

    em.close();
    emf.close();
}

}

<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="financas">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>br.com.laisa.financas.modelo.Conta</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/financas" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="laisaifce" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>



</persistence>

Olá Laisa!

Seu erro me parece semelhante a um tópico que respondi recentemente:

https://cursos.alura.com.br/forum/topico-conetando-com-mysql-77098

Vê se a solução dele funciona pra ti!

Espero que te ajude! ^^

Aguardo seu retorno! :D

Nao deu certo , coloquei esse conector e deu um erro maior , retornei para o meu conector 8.0 e alterei a url mas agora a conexao nem chega a rodar , ja da exception.

<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="financas">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>br.com.laisa.financas.modelo.Conta</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/financas?serverTimezone=UTC" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="laisaifce" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>

</persistence>

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named financas?serverTimezone=UTC at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) at br.com.laisa.financas.Teste.TestaConta.main(TestaConta.java:19)

vou desistir desse curso.

Laisa, bom dia, qual a versão do mysql que está usando?

ola bom dia!

versao 8.0.12

C:\Users\laisa>mysql -u root -p
Enter password: *********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 8.0.12 MySQL Community Server - GPL

mysql> use financas Database changed mysql> show tables; +--------------------+ | Tables_in_financas | +--------------------+ | conta | +--------------------+ 1 row in set (0.20 sec)

mysql> select *from conta; Empty set (0.03 sec)

mysql>

Alguem??

Tenta mudar o driver para:

<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.driver" />