4 Replies Latest reply on Feb 22, 2006 12:08 PM by java_tek_2006

    persisting entities using OneToMany relation

    java_tek_2006

      I am using two entities with a OneToMany relation . And the Child class have composite primary key with a foreign key from the parent PK.

      @Entity

      public class Parent {

      @Id
      private Integer Id

      @OneToMany
      Collection children;

      }

      @Entity

      public class Child {

      @EmbeddedId
      @AttributeOverrides( {
      @AttributeOverride(name = "Id", column = @Column(name = ID)),
      @AttributeOverride(name = "KeyId", column = @Column(name = OPTIONAL_INFO_KEY_ID)) })
      private ChildPK pk;
      }

      }


      Now when i call
      parent.setChildren(childList);
      entityManager.persist(parent); with a defined OneToMany relation in the parent class i was expecting the child records to be inserted along with th e parent record in the DB. But instead i see an update SQL generated for child table. ? Can anyone help me find out how i can insert both parent and child record in the DB using OneToMany relation ..

      Thanks

        • 1. Re: persisting entities using OneToMany relation
          epbernard

          are you sure you don't already have a child with the same PK in the database?

          • 2. Re: persisting entities using OneToMany relation
            java_tek_2006

            No ..There is no child record inserted in to database.

            only the parent record is inserted. And strangely an update is being called on the child table with reference to the record which is never inserted !

            update child set id=? where id = ? and keyId=?

            here id and keyId are the composite PK for the child table.

            • 3. Re: persisting entities using OneToMany relation
              epbernard

              So what would be great is to set up a minimal test case and post it here
              http://opensource2.atlassian.com/projects/hibernate/secure/Dashboard.jspa

              I think I fixed the issue in CVS but I was never able to reproduce it, so I'm not sure.

              • 4. Re: persisting entities using OneToMany relation
                java_tek_2006

                Hi ,

                Thanks for you reply.

                I would like to know whether it is possible to persist the parent child entities and expect the results that i am expecting when:

                1. child has a composite primary key that has a FK to the parent's table

                PK : keyId + id (parent table's primary key)

                2. "id" which is a FK and part of child's PK is a sequence number and is generated only when the parent record is inserted in the DB.

                3. Both keyId and id are non nullable fields of the child table.

                4. when i define bi-directional relation i don't see any exceptions and there is no insert SQL generated for the child table. but when i define only @OneToMany in parent entity i see the parent record insert SQL and update SQL for the child record which may be the update for the parent child relation. and an exception soon after the update is executed.
                following is the stack trace..

                [SQL] update EVENT_INFO set ID=? where ID=? and keyId=?
                [AbstractBatcher] preparing statement
                [IntegerType] binding '885218' to parameter: 1
                11:53:32,959 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
                org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.xo.olb.business.entity.EventInfo
                at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
                at org.hibernate.type.EntityType.getIdentifier(EntityType.java:107)
                at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:65)
                at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:654)
                at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:927)
                .......

                but where is the insert record for the child gone..?

                Also Could you please give me the details of the bug you were refering to ..?

                Thanks