4 Replies Latest reply on Feb 24, 2006 8:21 PM by tpedone

    Hibernate not copying primary key into associated object

    tpedone

      I have the following two objects and relationships:

      CoreMessage->CoreMessagePayload.

      Here's the relavent snippets of CoreMessage (the id and the payload reference)

       @Id(generate=GeneratorType.SEQUENCE, generator="SEQ_STORE")
       @Column(name="messageID", nullable=false)
       public Long getId()
       {
       return mId;
       }
      
       @OneToOne (cascade=CascadeType.ALL)
       @PrimaryKeyJoinColumn
       public CoreMessagePayload getPayload()
       {
       return mPayload;
       }
      


      And the relavent code for CoreMessagePayload:
       @Id(generate=GeneratorType.NONE)
       @Column(name="messageId", nullable=false)
       public Long getId()
       {
       return mId;
       }
      
      


      Aside from the @Column annotation, this is the same as the Person->Heart assocation in the docs:
      @Entity
      public class Body {
       @Id
       public Long getId() { return id; }
      
       @OneToOne(cascade = CascadeType.ALL)
       @PrimaryKeyJoinColumn
       public Heart getHeart() {
       return heart;
       }
       ...
      }
      
      @Entity
      public class Heart {
       @Id(generate = GeneratorType.NONE)
       public Long getId() { ...}
      }
      
      


      when I try to persist the CoreMessage, the CoreMessagePayload gets stored with 0 for the id (the id is intialized to 0 in the declaration) instead of getting the messageId from CoreMessage. The rest of the data for both objects gets stored correctly.

      Do I have to store the CoreMessage first, then copy the id into the CoreMessagePayload? I wouldn't think so, especially with Cascade.All.

      I'm using JBoss 4.0.3SP1 and EJB3-RC3.

      Any ideas?

      Thanks,

      Tim

        • 1. Re: Hibernate not copying primary key into associated object
          epbernard

          une @GenericGenerator and foreign

          • 2. Re: Hibernate not copying primary key into associated object
            tpedone

            Thanks.

            That worked I had to create an inverse One to One relationship from the CoreMessagePayload to the CoreMessage. Then tell the CoreMessagePayload to use the id of the referenced CoreMessage object.

            • 3. Re: Hibernate not copying primary key into associated object
              dbudworth

              tpedone, could you possibly post the relevant tags you put on your classes to get this to work?

              Spent the last 5 hours spinning my wheels on getting true OneToOnes working (where child pk = fk to parent pk).

              After trying combinations of @GenericGenerator(foreign) placed all around ( I can't find an example that says where the actual annotation belongs, so I tried the pk of the child with no luck).

              I eventually gave up and decided to redo all my tables to use sequence pk for child and plain old unique fk to parent. Which seems to still not work. (entity manager attempts to insert null in fk column even though i set parent.setChild(ch) and child.setParent(pa) in the code that creates the entities.

              Thanks

              • 4. Re: Hibernate not copying primary key into associated object
                tpedone

                Here's the id of the CoreMessagePayload:

                @Id(generate=GeneratorType.SEQUENCE, generator="SEQ_STORE")
                @Column(name="MESSAGEID", nullable=false)
                public Long getId()
                {
                return mId;
                }

                Notice that it is using the MessageID sequence we had originally defined for the Message. Then the Message class defines it's id as:

                @Id (generator="fk")
                @GenericGenerator(name="fk", strategy = "foreign",
                parameters = {
                @Parameter(name="property", value = "payload")
                }
                )
                @Column(name="messageID", nullable=false)
                public Long getId()
                {
                return mId;
                }

                Finally, the message has a reference to a MessagePayload:

                @OneToOne(cascade=CascadeType.ALL)
                @PrimaryKeyJoinColumn
                public CoreMessagePayload getPayload()
                {
                return mPayload;
                }

                Couple of things to note:

                1) We're still using EJB 3.0 RC3, I belive the Generator tags have changed since then.
                2) We changed our object model so that Message and MessagePayload are true one to one. Before, you could have a message without a payload. Now you can't. That let us define generate the id in the Payload instead of the Message since you can only use the foreign strategy from the entity that does not generate the pk.

                Tim