1 Reply Latest reply on Jun 23, 2009 1:33 PM by Dylan Janeke

    EntityHome and Foreign Id Generator errors on OneToOne associations.

    Dylan Janeke Newbie
      Hi All,

      I currently use the EntityHome, and EntityQuery design pattern.
      I have to entities like this :

      @Entity
      public class PersonEntity {
         @Id
         @Column(name="person_num")
         String personNumber;
        
         @OneToOne( cascade=CascadeType.ALL )
         @PrimaryKeyJoinColumn
         PersonProfileEntity profile;
        
         String name;
      }



      @Entity
      public class PersonProfileEntity {
         @Id
         @GeneratedValue(generator="fk_person_profile_pk")
         @GenericGenerator(name="fk_person_profile_pk", strategy="foreign" ,parameters={
             @Parameter(name="property", value="person") } )
         @Column(name = "person_id")
         Integer personId;
        
         @OneToOne( mappedBy="profile", optional=false )
         private PersonEntity person;

         @OneToMany(.....)
         List<CarEntity> cars;

         String password;
      }


      More on this I have a page that edits the PersonProfileEntity and PersonEntity in the same for assigning the values in the text boxes to the fields from the entityHome's defined instance.

      for ex :

      <h:form>
        <h:inputText value="#{personEntityHome.instance.personNumber}"/>
        <h:inputText value="#{personEntityHome.instance.name}"/>
        <h:inputText value="#{personEntityHome.instance.profile.password}"/>
        <h:commandButton id="save" value="Save" action="#{personEntityHome.persist}"
      </h:form>

      Know the idea is that when I click the 'Save' button it should save the PersonEntity and Cascade save the PersonProfileEntity, the id on the profile needs to be set to the same primary id as on the PersonEntity (personNumber).

      But I fail and keep getting this error :

      12:10:58,158 ERROR [application] javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: person
      javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: person
           at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
           at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
           at javax.faces.component.UICommand.broadcast(UICommand.java:387)


      Sorry for the plaintext post I couldn't figure out the formatting..:-( --> Seems to not be my day today..
        • 1. Re: EntityHome and Foreign Id Generator errors on OneToOne associations.
          Dylan Janeke Newbie
          Never mind guys.. I got it working...
          I just did this :

          @Override
          protected PersonEntity createInstance() {
                    log.info( "create instance() --->" );          
                          PersonEntity person = new PersonEntity();
                    PersonProfileEntity profile = new PersonProfileEntity();
                    profile.setPerson( person );
                    person.setProfile( profile );
                          return person;
          }

          I was just being stupid :P