Boa tarde pessoal estou tentando fazer o one-to-one usando annotations e não consegui resolver ainda, alguém por favor pode me ajudar…??
TABELAS:
Cliente:
CREATE TABLE `alberto`.`cliente` (
`id_cliente` int(11) NOT NULL auto_increment,
`cpf` bigint(20) NOT NULL,
`nome` char(40) NOT NULL,
`total_compras` decimal(16,2) default NULL,
`data_cadastro` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`data_nascimento` date default NULL,
PRIMARY KEY (`id_cliente`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Coracao:
CREATE TABLE `alberto`.`coracao` (
`id_cliente` int(11) NOT NULL auto_increment,
`corDoSangue` char(40) NOT NULL,
PRIMARY KEY (`id_cliente`),
CONSTRAINT `coracao_cliente_fk` FOREIGN KEY (`id_cliente`) REFERENCES `cliente` (`id_cliente`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Classe Cliente
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn(name = "id_cliente")
private Coracao coracao;
Classe Coracao
@Id
@Column(name = "id_cliente")
private int id;
Teste
public void testSaveClienteComCoracao() {
Cliente cliente = new Cliente();
cliente.setNome("Silvio");
cliente.setCpf(3296896);
cliente.setDataCadastro(new Date());
cliente.setDataNascimento(new Date());
cliente.setTemporaria(10);
cliente.setTotalCompras(12.50);
Coracao coracao = new Coracao();
coracao.setCorDoSangue("azul");
cliente.setCoracao(coracao);
Transaction tx = session.beginTransaction();
session.save(cliente);
tx.commit();
}
Erro:
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`alberto/coracao`, CONSTRAINT `coracao_exemplosilvio_fk` FOREIGN KEY (`id_cliente`) REFERENCES `exemplosilvio` (`id_cliente`))
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:647)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
[]'s e obrigado…