-->
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.  [ 3 posts ] 
Author Message
 Post subject: Can't get OneToMany annotation to work
PostPosted: Thu Mar 17, 2005 5:24 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
Does anyone have any idea what I'm doing wrong here. I can't seem to get the OneToMany annotation to work which I find surprising because I'd swear I had it working on another project. The problem is that the column that is used to link the two tables together is being set to null rather than the id of the row in the One table. Any help would be much appreciated as I'm not sure what else to try.

Hibernate version: hibernate-3.0rc1, hibernate-annotations-3.0alpha3

Mapping documents:

Code:
package test;

import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.CascadeType;
import javax.ejb.Entity;
import javax.ejb.GeneratorType;
import javax.ejb.Id;
import javax.ejb.JoinColumn;
import javax.ejb.OneToMany;

@Entity
public class One {
   private int id;
   private Collection<Many> many = new ArrayList<Many>();
   
   @Id(generate=javax.ejb.GeneratorType.IDENTITY)
   public int getId() {
      return id;
   }

   @OneToMany(cascade=CascadeType.CREATE)
   @JoinColumn(name="oneId")
   public Collection<Many> getMany() {
      return many;
   }

   public void setId(int id) {
      this.id = id;
   }

   public void setMany(Collection<Many> many) {
      this.many = many;
   }
}


Code:
package test;

import javax.ejb.Entity;
import javax.ejb.GeneratorType;
import javax.ejb.Id;
import javax.ejb.JoinColumn;
import javax.ejb.ManyToOne;

@Entity
public class Many {
   private int id;
   private One one;
   
   @Id(generate=javax.ejb.GeneratorType.IDENTITY)
   public int getId() {
      return id;
   }

   @ManyToOne
   @JoinColumn(name="oneId")
   public One getOne() {
      return one;
   }

   public void setId(int id) {
      this.id = id;
   }

   public void setOne(One one) {
      this.one = one;
   }
}


Code between sessionFactory.openSession() and session.close():

Code:
Transaction t = s.beginTransaction();
Many m = new Many();
One o = new One();
o.getMany().add(m);
s.persist(o);
t.commit();


Name and version of the database you are using:

MySQL 4.1.10 with InnoDB tables

Debug level Hibernate log excerpt:

Code:
Mar 17, 2005 2:50:17 PM org.hibernate.jdbc.AbstractBatcher log
FINE: insert into One values ( )
Mar 17, 2005 2:50:17 PM org.hibernate.jdbc.AbstractBatcher log
FINE: insert into Many (oneId) values (?)
Mar 17, 2005 2:50:17 PM org.hibernate.type.NullableType nullSafeSet
FINEST: binding null to parameter: 1


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 5:59 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
Here's a fuller debug log. As you can see, it's reading the identity value out of the One insert correctly. It's just not putting that value in the Many insert.

Code:
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.transaction.JDBCTransaction begin
     [java] FINE: begin
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.jdbc.AbstractBatcher openConnection
     [java] FINE: opening JDBC connection
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.connection.DriverManagerConnectionProvider getConnection
     [java] FINEST: total checked-out connections: 0
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.connection.DriverManagerConnectionProvider getConnection
     [java] FINEST: using pooled JDBC connection, pool size: 0
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.transaction.JDBCTransaction begin
     [java] FINE: current autocommit status: false
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades$IdentifierValue isUnsaved
     [java] FINEST: id unsaved-value: 0
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractSaveEventListener getEntityState
     [java] FINEST: transient instance of: test.One
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.DefaultPersistEventListener entityIsTransient
     [java] FINEST: saving transient instance
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractSaveEventListener saveWithGeneratedId
     [java] FINE: generated identifier: , using strategy: org.hibernate.id.IdentityGenerator
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractSaveEventListener performSave
     [java] FINEST: saving [test.One#<null>]
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractSaveEventListener performSaveOrReplicate
     [java] FINEST: executing insertions
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades cascade
     [java] FINEST: processing cascade ACTION_PERSIST for: test.One
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades cascade
     [java] FINEST: done processing cascade ACTION_PERSIST for: test.One
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.WrapVisitor processArrayOrNewCollection
     [java] FINEST: Wrapped collection in role: test.One.many
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.persister.entity.BasicEntityPersister insert
     [java] FINEST: Inserting entity: test.One (native id)
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.jdbc.AbstractBatcher logOpenPreparedStatement
     [java] FINE: about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.jdbc.AbstractBatcher log
     [java] FINE: insert into One values ( )
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.jdbc.AbstractBatcher getPreparedStatement
     [java] FINEST: preparing statement
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.persister.entity.BasicEntityPersister dehydrate
     [java] FINEST: Dehydrating entity: [test.One#<null>]
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.id.IdentifierGeneratorFactory getGeneratedIdentity
     [java] FINE: Natively generated identity: 3
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.jdbc.AbstractBatcher logClosePreparedStatement
     [java] FINE: about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.jdbc.AbstractBatcher closePreparedStatement
     [java] FINEST: closing statement
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades cascade
     [java] FINEST: processing cascade ACTION_PERSIST for: test.One
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades cascadeCollection
     [java] FINEST: cascade ACTION_PERSIST for collection: test.One.many
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades$8 cascade
     [java] FINEST: cascading to persist: test.Many
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades$IdentifierValue isUnsaved
     [java] FINEST: id unsaved-value: 0
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractSaveEventListener getEntityState
     [java] FINEST: transient instance of: test.Many
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.DefaultPersistEventListener entityIsTransient
     [java] FINEST: saving transient instance
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractSaveEventListener saveWithGeneratedId
     [java] FINE: generated identifier: , using strategy: org.hibernate.id.IdentityGenerator
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractSaveEventListener performSave
     [java] FINEST: saving [test.Many#<null>]
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractSaveEventListener performSaveOrReplicate
     [java] FINEST: executing insertions
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.persister.entity.BasicEntityPersister insert
     [java] FINEST: Inserting entity: test.Many (native id)
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.jdbc.AbstractBatcher logOpenPreparedStatement
     [java] FINE: about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.jdbc.AbstractBatcher log
     [java] FINE: insert into Many (oneId) values (?)
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.jdbc.AbstractBatcher getPreparedStatement
     [java] FINEST: preparing statement
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.persister.entity.BasicEntityPersister dehydrate
     [java] FINEST: Dehydrating entity: [test.Many#<null>]
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.type.NullableType nullSafeSet
     [java] FINEST: binding null to parameter: 1
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.id.IdentifierGeneratorFactory getGeneratedIdentity
     [java] FINE: Natively generated identity: 3
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.jdbc.AbstractBatcher logClosePreparedStatement
     [java] FINE: about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.jdbc.AbstractBatcher closePreparedStatement
     [java] FINEST: closing statement
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades cascadeCollection
     [java] FINEST: done cascade ACTION_PERSIST for collection: test.One.many
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades cascade
     [java] FINEST: done processing cascade ACTION_PERSIST for: test.One
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.transaction.JDBCTransaction commit
     [java] FINE: commit
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.impl.SessionImpl managedFlush
     [java] FINEST: automatically flushing session
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractFlushingEventListener flushEverythingToExecutions
     [java] FINEST: flushing session
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractFlushingEventListener prepareEntityFlushes
     [java] FINE: processing flush-time cascades
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades cascade
     [java] FINEST: processing cascade ACTION_SAVE_UPDATE for: test.One
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades cascadeCollection
     [java] FINEST: cascade ACTION_SAVE_UPDATE for collection: test.One.many
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades$5 cascade
     [java] FINEST: cascading to saveOrUpdate: test.Many
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractSaveEventListener getEntityState
     [java] FINEST: persistent instance of: test.Many
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.DefaultSaveOrUpdateEventListener entityIsPersistent
     [java] FINEST: ignoring persistent instance
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.DefaultSaveOrUpdateEventListener entityIsPersistent
     [java] FINEST: object already associated with session: [test.Many#3]
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades cascadeCollection
     [java] FINEST: done cascade ACTION_SAVE_UPDATE for collection: test.One.many
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.engine.Cascades cascade
     [java] FINEST: done processing cascade ACTION_SAVE_UPDATE for: test.One
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractFlushingEventListener prepareCollectionFlushes
     [java] FINE: dirty checking collections
     [java] Mar 17, 2005 3:10:42 PM org.hibernate.event.def.AbstractFlushingEventListener flushEntities
     [java] FINEST: Flushing entities and processing referenced collections
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.engine.Collections updateReachableCollection
     [java] FINE: Collection found: [test.One.many#3], was: [<unreferenced>] (initialized)
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.event.def.AbstractFlushingEventListener flushCollections
     [java] FINEST: Processing unreferenced collections
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.event.def.AbstractFlushingEventListener flushCollections
     [java] FINEST: Scheduling collection removes/(re)creates/updates
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.event.def.AbstractFlushingEventListener flushEverythingToExecutions
     [java] FINE: Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.event.def.AbstractFlushingEventListener flushEverythingToExecutions
     [java] FINE: Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.pretty.Printer toString
     [java] FINE: listing entities:
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.pretty.Printer toString
     [java] FINE: test.Many{one=null, id=3}
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.pretty.Printer toString
     [java] FINE: test.One{many=[test.Many#3], id=3}
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
     [java] FINEST: executing flush
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.event.def.AbstractFlushingEventListener postFlush
     [java] FINEST: post flush
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.jdbc.JDBCContext beforeTransactionCompletion
     [java] FINEST: before transaction completion
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.impl.SessionImpl beforeTransactionCompletion
     [java] FINEST: before transaction completion
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.transaction.JDBCTransaction commit
     [java] FINE: committed JDBC Connection
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.jdbc.JDBCContext afterTransactionCompletion
     [java] FINEST: after transaction completion
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.impl.SessionImpl afterTransactionCompletion
     [java] FINEST: after transaction completion
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.impl.SessionImpl close
     [java] FINEST: closing session
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.jdbc.AbstractBatcher closeConnection
     [java] FINE: closing JDBC connection (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.connection.DriverManagerConnectionProvider closeConnection
     [java] FINEST: returning connection to pool, pool size: 1
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.jdbc.JDBCContext afterTransactionCompletion
     [java] FINEST: after transaction completion
     [java] Mar 17, 2005 3:10:43 PM org.hibernate.impl.SessionImpl afterTransactionCompletion
     [java] FINEST: after transaction completion


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 18, 2005 5:50 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Strange, can you set a runnable test case and pput in in JIRA?

_________________
Emmanuel


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