Hello,
I am new to hibernate and found myself on a strange problem.
I'm using Hibernate 3.2.1 with annotations.
The problem is as follows: I have
Code:
@Entity
public class OperationType extends DomainObject {
private Collection<BankOperation> transactions = new ArrayList<BankOperation>();
@OneToMany(mappedBy="operationType",cascade={CascadeType.ALL})
public Collection<BankOperation> getTransactions() {
return transactions;
}
public void setTransactions(Collection<BankOperation> transactions) {
this.transactions = transactions;
}
}
and
Code:
@Entity
public class BankOperation extends DomainObject {
private OperationType operationType;
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name= "operationTypeId")
public OperationType getOperationType() {
return operationType;
}
public void setOperationType(OperationType type) {
this.operationType = type;
}
The problem is when I do the following:
Code:
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
OperationType t1 = new OperationType();
t1.setDesignation("t1");
BankOperation o = new BankOperation();
o.setOperationType(t1);
t1.getTransactions().add(o);
session.saveOrUpdate(o);
session.getTransaction().commit();
}
all entities are correctly persisted into the database, but the "operationTypeId" column in "BankOperation" table is allways ZERO (the primary key for the inserterd operationtype is ONE).
Am I missing something? I've read the documentation more than once and it actually helped me before, but I'm kind of lost right now.
Best Regards.