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