Solucionado (ver solução)
Solucionado
(ver solução)
10
respostas

no modulo jpa no capitulo mapeamento entre classes

nao ta executando a class 
 package br.com.caelum.financas.teste;

import java.math.BigDecimal;
import java.util.Calendar;

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

import br.com.caelum.financas.modelo.Conta;
import br.com.caelum.financas.modelo.Movimentacao;
import br.com.caelum.financas.modelo.TipoMovimentacao;
import br.com.caelum.financas.util.JPAUtil;

public class TesteJPARelacionamento {

    public static void main(String[] args) {

        double inicio = System.currentTimeMillis();
        Conta conta = new Conta();
        conta.setId(3);
        conta.setTitular("Maria santos");
        conta.setBanco("itau");
        conta.setNumero("69720");
        conta.setAgencia("065");

        Movimentacao movimentacao = new Movimentacao();
        movimentacao.setData(Calendar.getInstance());
        movimentacao.setDescricao("Conta de luz");
        movimentacao.setTipoMovimentacao(TipoMovimentacao.SAIDA);
        movimentacao.setValor(new BigDecimal("123.9"));

        movimentacao.setConta(conta);

        EntityManager manager = new JPAUtil().getEntityManager();
        manager.getTransaction().begin();

        manager.persist(conta);

        manager.persist(movimentacao);
        System.out.println(conta.getId());



        conta.setTitular("Mauricio Ramos");

        manager.getTransaction().commit();

        manager.close();

        double fim  = System.currentTimeMillis();
                System.out.println("Executado em: "+(fim - inicio)/1000 + "s");
    }

}
package br.com.caelum.financas.teste;

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

import br.com.caelum.financas.modelo.Conta;
import br.com.caelum.financas.util.JPAUtil;

public class TesteJPS {

    public static void main(String[] args) {

        double inicio = System.currentTimeMillis();
        Conta conta = new Conta();
        conta.setId(3);
        conta.setTitular("Maria santos");
        conta.setBanco("itau");
        conta.setNumero("69720");
        conta.setAgencia("065");

        EntityManager manager = new JPAUtil().getEntityManager();
        manager.getTransaction().begin();

        manager.merge(conta);
        System.out.println(conta.getId());



        conta.setTitular("Mauricio Ramos");

        manager.getTransaction().commit();

        manager.close();

        double fim  = System.currentTimeMillis();
                System.out.println("Executado em: "+(fim - inicio)/1000 + "s");
    }

}
package br.com.caelum.financas.modelo;

public enum TipoMovimentacao {
    ENTRADA,SAIDA;

}
package br.com.caelum.financas.modelo;

import java.math.BigDecimal;
import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;



@Entity
public class Movimentacao {

    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    private BigDecimal valor;
    @Enumerated(EnumType.STRING)
    private TipoMovimentacao tipoMovimentacao;

    @Temporal(TemporalType.DATE)
    private Calendar data;
    private String descricao;
    @ManyToOne
    private Conta conta;

    public Conta getConta() {
        return conta;
    }

    public void setConta(Conta conta) {
        this.conta = conta;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public BigDecimal getValor() {
        return valor;
    }

    public void setValor(BigDecimal valor) {
        this.valor = valor;
    }

    public TipoMovimentacao getTipoMovimentacao() {
        return tipoMovimentacao;
    }

    public void setTipoMovimentacao(TipoMovimentacao tipoMovimentacao) {
        this.tipoMovimentacao = tipoMovimentacao;
    }

    public Calendar getData() {
        return data;
    }

    public void setData(Calendar data) {
        this.data = data;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }



}
erro na console eh o seguinte:
set 18, 2014 8:50:52 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
set 18, 2014 8:50:52 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
set 18, 2014 8:50:52 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
set 18, 2014 8:50:52 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: financas
    ...]
set 18, 2014 8:50:53 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
set 18, 2014 8:50:53 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
set 18, 2014 8:50:53 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
set 18, 2014 8:50:53 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
set 18, 2014 8:50:53 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
set 18, 2014 8:50:53 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/financas]
set 18, 2014 8:50:53 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
set 18, 2014 8:50:53 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
set 18, 2014 8:50:53 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
set 18, 2014 8:50:58 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
set 18, 2014 8:50:58 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
set 18, 2014 8:50:59 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
set 18, 2014 8:51:00 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
set 18, 2014 8:51:00 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
set 18, 2014 8:51:00 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
set 18, 2014 8:51:01 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: financas.conta
set 18, 2014 8:51:01 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [numero, banco, id, agencia, titular]
set 18, 2014 8:51:01 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
set 18, 2014 8:51:01 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
set 18, 2014 8:51:01 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: financas.movimentacao
set 18, 2014 8:51:01 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [data, tipomovimentacao, conta_id, valor, id, descricao]
set 18, 2014 8:51:01 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: [fk_7o9440vw5hvyvd59ngil2gwbs]
set 18, 2014 8:51:01 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [fk_7o9440vw5hvyvd59ngil2gwbs, primary]
set 18, 2014 8:51:01 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: br.com.caelum.financas.modelo.Conta
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1187)
    at br.com.caelum.financas.teste.TesteJPARelacionamento.main(TesteJPARelacionamento.java:38)
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: br.com.caelum.financas.modelo.Conta
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:139)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181)
    ... 1 more
10 respostas
solução!

Olá Gustavo

Na sua classe TesteJPARelacionamento você não pode colocar um id no objeto que é passado para o método persist do EntityManager, pois ele considera esse objeto como no estado Detached e por isso não pode ser persistido no banco de dados.

Para corrigir o seu código, remova a linha conta.setId(3) do método main da classe TesteJPARelacionamento.

Ola, aoexecutar o PopulaMovimentacao da esse erro no console

set 19, 2014 2:15:12 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
set 19, 2014 2:15:12 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.0.Final}
set 19, 2014 2:15:12 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
set 19, 2014 2:15:12 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
set 19, 2014 2:15:14 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
set 19, 2014 2:15:14 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
set 19, 2014 2:15:14 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
set 19, 2014 2:15:14 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/financas]
set 19, 2014 2:15:14 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, autocommit=true, release_mode=auto}
set 19, 2014 2:15:15 PM org.hibernate.engine.jdbc.internal.JdbcServicesImpl configure
WARN: HHH000342: Could not obtain connection to query metadata : Access denied for user 'root'@'localhost' (using password: NO)
set 19, 2014 2:15:15 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
set 19, 2014 2:15:15 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
set 19, 2014 2:15:15 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
set 19, 2014 2:15:15 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
set 19, 2014 2:15:16 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
set 19, 2014 2:15:16 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
set 19, 2014 2:15:16 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000319: Could not get database metadata
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2483)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:204)
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:505)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:920)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:72)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at br.com.caelum.financas.util.JPAUtil.<clinit>(JPAUtil.java:10)
    at br.com.caelum.financas.util.PopulaConta.main(PopulaConta.java:11)

set 19, 2014 2:15:16 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000299: Could not complete schema update
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2483)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:204)
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:505)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:920)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:72)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at br.com.caelum.financas.util.JPAUtil.<clinit>(JPAUtil.java:10)
    at br.com.caelum.financas.util.PopulaConta.main(PopulaConta.java:11)

set 19, 2014 2:15:17 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1045, SQLState: 28000
set 19, 2014 2:15:17 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Access denied for user 'root'@'localhost' (using password: NO)
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 br.com.caelum.financas.util.PopulaConta.main(PopulaConta.java:13)
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:1425)
    at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:59)
    ... 1 more
Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2483)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    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
package br.com.caelum.financas.util;

import java.math.BigDecimal;
import java.util.Calendar;

import javax.persistence.EntityManager;

import br.com.caelum.financas.modelo.Conta;
import br.com.caelum.financas.modelo.Movimentacao;
import br.com.caelum.financas.modelo.TipoMovimentacao;

public class PopulaMovimentacao {

    public static void main(String[] args) {

        EntityManager manager = new JPAUtil().getEntityManager();

        manager.getTransaction().begin();

        Conta conta1 = manager.find(Conta.class,1);
        Conta conta2 = manager.find(Conta.class,2);
        Conta conta3 = manager.find(Conta.class,3);
        Conta conta4 = manager.find(Conta.class,4);
        Conta conta5 = manager.find(Conta.class,5);


        // Movimentacoes da conta1
        Movimentacao movimentacao1 = new Movimentacao();
        Movimentacao movimentacao2 = new Movimentacao();
        Movimentacao movimentacao3 = new Movimentacao();
        Movimentacao movimentacao4 = new Movimentacao();

        movimentacao1.setData(Calendar.getInstance());
        movimentacao1.setDescricao("Conta de luz - ABRIL/2012");
        movimentacao1.setValor(new BigDecimal("135"));
        movimentacao1.setTipoMovimentacao(TipoMovimentacao.SAIDA);
        movimentacao1.setConta(conta1);

        manager.persist(movimentacao1);

        movimentacao2.setData(Calendar.getInstance());
        movimentacao2.setDescricao("Almoco no Restaurante - AGOSTO/2012");
        movimentacao2.setValor(new BigDecimal("175.80"));
        movimentacao2.setTipoMovimentacao(TipoMovimentacao.SAIDA);
        movimentacao2.setConta(conta1);

        manager.persist(movimentacao2);

        movimentacao3.setData(Calendar.getInstance());
        movimentacao3.setDescricao("Aluguel - MAIO/2012");
        movimentacao3.setValor(new BigDecimal("680.00"));
        movimentacao3.setTipoMovimentacao(TipoMovimentacao.ENTRADA);
        movimentacao3.setConta(conta1);

        manager.persist(movimentacao3);

        movimentacao4.setData(Calendar.getInstance());
        movimentacao4.setDescricao("Salario - FEVEREIRO/2012");
        movimentacao4.setValor(new BigDecimal("3830.68"));
        movimentacao4.setTipoMovimentacao(TipoMovimentacao.ENTRADA);
        movimentacao4.setConta(conta1);

        manager.persist(movimentacao4);

        // Movimentacoes da conta2
        Movimentacao movimentacao5 = new Movimentacao();
        Movimentacao movimentacao6 = new Movimentacao();

        movimentacao5.setData(Calendar.getInstance());
        movimentacao5.setDescricao("Conta de telefone - SETEMBRO/2011");
        movimentacao5.setValor(new BigDecimal("168.27"));
        movimentacao5.setTipoMovimentacao(TipoMovimentacao.SAIDA);
        movimentacao5.setConta(conta2);

        manager.persist(movimentacao5);

        movimentacao6.setData(Calendar.getInstance());
        movimentacao6.setDescricao("Aniversario - MAIO/2011");
        movimentacao6.setValor(new BigDecimal("200"));
        movimentacao6.setTipoMovimentacao(TipoMovimentacao.ENTRADA);
        movimentacao6.setConta(conta2);

        manager.persist(movimentacao6);

        // Movimentacoes da conta3
        Movimentacao movimentacao7 = new Movimentacao();
        Movimentacao movimentacao8 = new Movimentacao();
        Movimentacao movimentacao9 = new Movimentacao();

        movimentacao7.setData(Calendar.getInstance());
        movimentacao7.setDescricao("Lanche - JULHO/2011");
        movimentacao7.setValor(new BigDecimal("28.50"));
        movimentacao7.setTipoMovimentacao(TipoMovimentacao.SAIDA);
        movimentacao7.setConta(conta3);

        manager.persist(movimentacao7);

        movimentacao8.setData(Calendar.getInstance());
        movimentacao8.setDescricao("Presente - DEZEMBRO/2011");
        movimentacao8.setValor(new BigDecimal("49.99"));
        movimentacao8.setTipoMovimentacao(TipoMovimentacao.SAIDA);
        movimentacao8.setConta(conta3);

        manager.persist(movimentacao8);

        movimentacao9.setData(Calendar.getInstance());
        movimentacao9.setDescricao("Bonus - JANEIRO/2012");
        movimentacao9.setValor(new BigDecimal("2000"));
        movimentacao9.setTipoMovimentacao(TipoMovimentacao.ENTRADA);
        movimentacao9.setConta(conta3);

        manager.persist(movimentacao9);

        // Movimentacoes da conta4
        Movimentacao movimentacao10 = new Movimentacao();

        movimentacao10.setData(Calendar.getInstance());
        movimentacao10.setDescricao("Carnaval - MARCO/2012");
        movimentacao10.setValor(new BigDecimal("765.20"));
        movimentacao10.setTipoMovimentacao(TipoMovimentacao.SAIDA);
        movimentacao10.setConta(conta4);

        manager.persist(movimentacao10);

        // Movimentacoes da conta5
        Movimentacao movimentacao11 = new Movimentacao();
        Movimentacao movimentacao12 = new Movimentacao();

        movimentacao11.setData(Calendar.getInstance());
        movimentacao11.setDescricao("Salario - ABRIL/2012");
        movimentacao11.setValor(new BigDecimal("2651.90"));
        movimentacao11.setTipoMovimentacao(TipoMovimentacao.ENTRADA);
        movimentacao11.setConta(conta5);

        manager.persist(movimentacao11);

        movimentacao12.setData(Calendar.getInstance());
        movimentacao12.setDescricao("Bonus - JANEIRO/2012");
        movimentacao12.setValor(new BigDecimal("1000"));
        movimentacao12.setTipoMovimentacao(TipoMovimentacao.ENTRADA);
        movimentacao12.setConta(conta5);

        manager.persist(movimentacao12);

        manager.getTransaction().commit();

        manager.close();

    }

}

Oi Gustavo

O outro problema foi resolvido? Essa classe que está lançando a exceção agora é do mesmo projeto?

sim o outro eu fui fazendo cm a video aula ai fui fazer o ultimo exercico e tive q deleta meu projeto e baixa o projeto de vc. mas parece que a class movimentacao nao ta criada. pelo menos nao veio no down

Oi Gustavo

Eu não entendi direito a sua resposta. As duas classes pertencem ao mesmo projeto?

Por que esse segundo problema que você está tendo acontece por que a JPA não está conseguindo se conectar ao banco de dados utilizando o usuário root e a senha vazia, ou seja, o projeto da classe PopulaMovimentacao não está com o persistence.xml configurado corretamente.

Eh o projeto financas 4 do ultimo exercício que esta pedindo

Q na parte explicação manda baixa

Gustavo, deu certo o código do Victor? Suas dúvidas foram sanadas?

Abraço

Olhando sua exceção vejo que o acesso ao banco não foi permitido. Você alterou a senha do banco?

Oi Gustavo, vou fechar o thread. Qq problema abre uma nova discussão.

abs