-->
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.  [ 4 posts ] 
Author Message
 Post subject: Annotations: OneToMany - ManyToOne, FK is always 'null'
PostPosted: Tue Mar 14, 2006 2:18 am 
Newbie

Joined: Mon Mar 13, 2006 10:19 am
Posts: 4
Location: Germany
Hi to all,

Hibernate version: 3.1.2
Hibernate annotations version: 3.1beta8
Name and version of the database: MySQL 5.0.19-nt

i am new to hibernate and have some problems with the annotation for an OneToMany <--> ManyToOne mapping.

The value of the foreign key field of the child records are always 'null' and do not carry the primary key of the parent record.

Here is my code:

Code of the parent class 'Firma.java'

Code:
@Entity
@Table (name="firmen")
public class Firma implements Serializable {
  private static final long serialVersionUID = -6665434043077843943L;

  private int firmaId;
  private String name;
  private List<Person> personen = new ArrayList<Person>();

  @Id @GeneratedValue(strategy = GenerationType.AUTO)
  @Column (name="FIRMA_ID")
  public int getFirmaId() {
    return firmaId;
  }
  public void setFirmaId(int firmaId) {
    this.firmaId = firmaId;
  }

  @Column (name="NAME", length=30)
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

  @OneToMany (targetEntity=Person.class, mappedBy="firma", cascade=CascadeType.ALL)
  public List<Person> getPersonen() {
    return personen;
  }
  public void setPersonen(List<Person> personen) {
    this.personen = personen;
  }


Code of the child class 'Person.java':
Code:
@Entity
@Table (name="personen")
public class Person implements Serializable {
  private static final long serialVersionUID = 2135362023133590877L;
  private int personId;
  private String name;
  private Firma firma;

  public static long getSerialVersionUID() {
    return serialVersionUID;
  }

  @Id @GeneratedValue(strategy = GenerationType.AUTO)   
  public int getPersonId() {
    return personId;
  }
  public void setPersonId(int personId) {
    this.personId = personId;
  }

  @Column (name="NAME", length=20)
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

  @ManyToOne (cascade=CascadeType.ALL)
  @JoinColumn (name="FIRMA_FK", referencedColumnName = "FIRMA_ID", insertable=false, updatable=false)
  public Firma getFirma() {
    return firma;
  }

  public void setFirma(Firma firma) {
    this.firma = firma;
  }
}


And this is the code of my main class:
Code:
  ...
  public static void main(String[] args) {
    Session session = HibernateUtil.currentSession();
    Transaction tx = session.beginTransaction();
   
    Firma firma = new Firma();
    firma.setName("Dummy Corp.");
   
    List<Person> personen = new ArrayList<Person>();
   
    Person p1 = new Person();
    p1.setName("Barnes");
    personen.add(p1);

    Person p2 = new Person();
    p2.setName("Smith");
    personen.add(p2);
   
    firma.setPersonen(personen);
    session.save(firma);
    session.flush();

    tx.commit();   
    session.close();
  }


Hibernate generates one 'firma' record and two 'person' records. At the 'person' entries the foreign key (FIRMA_FK) always is 'null'.

Thank´s a lot for your assistance.

Gerhard


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 5:23 am 
Beginner
Beginner

Joined: Wed Apr 13, 2005 10:34 am
Posts: 38
Since the association between person and firma is bidirectional, you have to set it on both sides. The person.setFirma(firma) is missing.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 7:32 am 
Newbie

Joined: Mon Mar 13, 2006 10:19 am
Posts: 4
Location: Germany
h.b. wrote:
Since the association between person and firma is bidirectional, you have to set it on both sides. The person.setFirma(firma) is missing.


Thank´s a lot for your hint. Could you please give me some additional explanations on your idea? There is just already a person.setFirma(firma) method in my 'Person' class.

Code:
  ...
  @ManyToOne (cascade=CascadeType.ALL)
  @JoinColumn (name="FIRMA_FK", referencedColumnName = "FIRMA_ID", insertable=false, updatable=false)
  public Firma getFirma() {
    return firma;
  }
  public void setFirma(Firma firma) {
    this.firma = firma;
  }
  ...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 9:23 am 
Beginner
Beginner

Joined: Wed Apr 13, 2005 10:34 am
Posts: 38
Yes, there is a setter, but you don't use it.
;-)

I'd recommend to use dedicated methods dealing with bidirectional associations. Direct access to collections should be protected.

Code:
class Firma
{
   protected Collection<Person> getPersonen(){...}
   protected void setPersonen(Collection<Person> ps){...)

   public boolean addPerson(Person p)
   {
      p.setFirma(this);
      return personen.add(p);
   }

   public boolean removePerson(Person p)
   {
      return personen.remove(p);
   }

   public Collection<Person> readPersonen()
   {
      return Collections.unmodifiableCollection(getPersonen());
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.