-->
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.  [ 1 post ] 
Author Message
 Post subject: Annotations: OneToMany - ManyToOne, FK is always 'null'
PostPosted: Mon Mar 13, 2006 2:23 pm 
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();
  }


ThankĀ“s a lot for your assistance.

Gerhard


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

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.