Hibernate: problema com foreign key

2 respostas
R

Oi.
Tenho uma dúvida sobre como criar chaves estrangeiras com o hibernate. Eu já dei uma procurada no fórum e achei alguns códigos, mas eles não funcionaram. Então vou postar meu código aqui. Agradeço se vocês puderem me ajudar

Eu tenho uma classe Cliente com o cpf como primary key e uma classe Pedido que eu gostaria de inserir o cpf como chave estrangeira

Cliente

package de.livraria;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Cliente {
	int cpf;
	String nome;
	String login;
	String senha;
	int telefone;
	
	@Id
	@Column(name = "CPF")
	public int getCpf() {
		return cpf;
	}
	public void setCpf(int cpf) {
		this.cpf = cpf;
	}
	@Column(name = "Nome", nullable = false, length = 50)
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	@Column(name = "Login", nullable = false, length = 50)
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	@Column(name = "Senha", nullable = false, length = 50)
	public String getSenha() {
		return senha;
	}
	public void setSenha(String senha) {
		this.senha = senha;
	}
	@Column(name = "Telefone", nullable = false, length = 50)
	public int getTelefone() {
		return telefone;
	}
	public void setTelefone(int telefone) {
		this.telefone = telefone;
	}	
}

Pedido

package de.livraria;


import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;

import org.hibernate.annotations.ForeignKey;

@Entity
public class Pedido {	
	int idPedido;
	Date data;
	String enderecoEntrega;
	
	
	@Id
	@Column(name = "IdPedido")
	public int getIdPedido() {
		return idPedido;
	}
	public void setIdPedido(int idPedido) {
		this.idPedido = idPedido;
	}
	@Column(name = "Data", nullable = false, length = 50)
	public Date getData() {
		return data;
	}
	public void setData(Date data) {
		this.data = data;
	}
	@Column(name = "EnderecoEntrega", nullable = false, length = 50)
	public String getEnderecoEntrega() {
		return enderecoEntrega;
	}
	public void setEnderecoEntrega(String enderecoEntrega) {
		this.enderecoEntrega = enderecoEntrega;
	}
	
	 @OneToOne  
	 @JoinColumn(name="CPF", referencedColumnName = "CPF")   
	 private Cliente cliente; 	 
}

2 Respostas

luxu

Como vc mapeou no banco? Post aki…

R

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

  <session-factory>
    

      <property name="connection.url">jdbc:mysql://localhost:3306/livrariaDigital</property>
      <property name="connection.username">root</property>
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="connection.password">r</property>

    <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
  
    <property name="current_session_context_class">thread</property>
   
    <property name="hibernate.show_sql">true</property>
 
    <property name="hibernate.hbm2ddl.auto">create</property>

    <mapping class="de.livraria.Cliente" />  
    <mapping class="de.livraria.Pedido" /> 

  </session-factory>

</hibernate-configuration>

Classe Principal

package de.livraria;


import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class TestExample {

	public static void main(String[] args) {
		AnnotationConfiguration config = new AnnotationConfiguration();
		config.addAnnotatedClass(Cliente.class);
		config.addAnnotatedClass(Pedido.class);	
		config.configure("hibernate.cfg.xml");
		
		new SchemaExport(config).create(true, true);
}  
}

E após executar fica assim:

create table Cliente (CPF integer not null, Nome varchar(50) not null, Telefone integer not null, Login varchar(50) not null, Senha varchar(50) not null, primary key (CPF))
create table Pedido (IdPedido integer not null, EnderecoEntrega varchar(50) not null, Data datetime not null, primary key (IdPedido))
12:25:21,515  INFO SchemaExport:268 - schema export complete
12:25:21,515  INFO DriverManagerConnectionProvider:170 - cleaning up connection pool: jdbc:mysql://localhost:3306/livrariaDigital

Mas ele não está criando a coluna CPF dentro da tabela Pedido

Criado 14 de novembro de 2011
Ultima resposta 15 de nov. de 2011
Respostas 2
Participantes 2