These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: OneToMany: Unidirectinal mapping: Bug or Mistake?
PostPosted: Sat Oct 22, 2011 8:32 am 
Beginner
Beginner

Joined: Wed Jun 28, 2006 2:24 pm
Posts: 30
Location: Brazil
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


Top
 Profile  
 
 Post subject: Re: OneToMany: Unidirectinal mapping: Bug or Mistake?
PostPosted: Mon Oct 24, 2011 2:07 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Basically, you have two options. Either allow null values in the id_funcionario column, or make the mapping bi-directional. The latter option is recommended and is explained in the the following Hibernate documentation. http://docs.jboss.org/hibernate/core/3. ... child.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.