Hi,
I'm getting the following exception when I try to persist an OneToMany unidirectional:
java.sql.SQLException: Field 'id_funcionario' doesn't have a default value
The field 'id_funcionario' is the foreign key and is defined as not null. If this field is defiined as nullable works.
Below are the classes and the DDL.
Code:
@Entity
public class Funcionario implements Serializable {
private Integer id;
private String nome;
private List<Telefone> telefones;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="id_funcionario")
public List<Telefone> getTelefones() {
return telefones;
}
public void setTelefones(List<Telefone> telefones) {
this.telefones = telefones;
}
}
@Entity
public class Telefone implements Serializable {
private Integer id;
private String numero;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNumero() {
return numero;
}
public void setNumero(String numero) {
this.numero = numero;
}
}
Code:
CREATE TABLE funcionario (
id int(11) NOT NULL AUTO_INCREMENT,
nome varchar(50) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE telefone (
id int(11) NOT NULL AUTO_INCREMENT,
numero varchar(15) NOT NULL,
id_funcionario int(11) NOT NULL,
PRIMARY KEY (id),
KEY telefone_fk1 (id_funcionario),
CONSTRAINT telefone_fk1 FOREIGN KEY (id_funcionario) REFERENCES funcionario (id)
);
This is a bug or my mistake?
Thanks,
Rafael Santini