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