-->
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: Foreign keys are inserted as zero
PostPosted: Tue Jan 23, 2007 8:01 pm 
Newbie

Joined: Tue Jan 23, 2007 7:52 pm
Posts: 2
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 7:04 am 
Newbie

Joined: Tue Jan 23, 2007 7:52 pm
Posts: 2
Hello,

I got the solution from an experienced Hibernate user.

The problem was in the DomainObject superclass, in the "id" attribute, that had not the @GeneratedValue tag.

I did not find this hint in Annotations documentation. My feeling is that Annotations documentation is less detailed when compared with Core documentation.

Thank you anyway.

Regards.


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.