Boa tarde pessoal, estou fazendo um exemplo de relacionamento OntToMany usando JPA e está acontecendo um erro que nao consegui resolver ainda, alguém pode me ajudar por favor:
Tabelas:
CREATE TABLE cliente (
identificador INTEGER NOT NULL AUTO_INCREMENT,
nome VARCHAR(45) NOT NULL,
PRIMARY KEY(identificador)
) ENGINE = InnoDB;
CREATE TABLE endereco (
identificador INTEGER NOT NULL AUTO_INCREMENT,
rua VARCHAR(45) NOT NULL,
identificador_cliente INTEGER NOT NULL,
PRIMARY KEY(identificador),
CONSTRAINT FK_cliente
FOREIGN KEY FK_endereco(identificador_cliente)
REFERENCES cliente (identificador)
ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB;
Model Cliente:
@Entity
public class Cliente {
private int id;
private String nome;
private List<Endereco> enderecos = new ArrayList<Endereco>();
@OneToMany(cascade = { CascadeType.PERSIST })
public List<Endereco> getEnderecos() {
return enderecos;
}
public void setEnderecos(List<Endereco> enderecos) {
this.enderecos = enderecos;
}
@Id
@Column(name = "identificador")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public void addEndereco(Endereco endereco) {
enderecos.add(endereco);
}
}
Model Endereco:
@Entity
public class Endereco {
public Endereco() {
}
public Endereco(String rua) {
this.rua = rua;
}
private int id;
private String rua;
@Id
@Column(name = "identificador")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRua() {
return rua;
}
public void setRua(String rua) {
this.rua = rua;
}
}
DAO:
public class BaseDAO<T> {
private static EntityManagerFactory mf;
private EntityManager em;
public BaseDAO() {
mf = Persistence.createEntityManagerFactory("pu1");
em = mf.createEntityManager();
}
public void salvar(T obj) {
em.getTransaction().begin();
try {
em.persist(obj);
em.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
em.getTransaction().rollback();
}
}
}
TesteDAO:
public class TesteDAO {
public static void main(String[] args) {
BaseDAO<Cliente> dao = new BaseDAO<Cliente>();
Endereco ed = new Endereco();
ed.setRua("Rua dos loucos");
Cliente c = new Cliente();
c.setNome("Silvio");
c.addEndereco(ed);
dao.salvar(c);
}
}
Erro:
Internal Exception: java.sql.SQLException: Cannot add or update a child row: a foreign key constraint fails (`unicodono/endereco`, CONSTRAINT `FK_cliente` FOREIGN KEY (`identificador_cliente`) REFERENCES `cliente` (`identificador`))Error Code: 1452
Call:INSERT INTO ENDERECO (RUA) VALUES (?)
bind => [Rua dos loucos]
Obrigado, estarei no aguardo pra quem puder me jaudar…
[]'s