Uploaded image for project: 'Spring Data JPA'
  1. Spring Data JPA
  2. DATAJPA-1304

attempted to assign id from null one-to-one property

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Works as Designed
    • Affects Version/s: 2.0.5 (Kay SR5)
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
    • Environment:
      Spring 5.0.4.RELEASE, Spring-Data 2.0.5.RELEASE, Hibernate 5.2.16.Final

      Description

      catch very unexpected exception while updgrading hibernate version 5.2.12 -> 5.2.15

      org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [models.Secunda.parent]
      

      in scenario when update object with non null OneToOne property.

      i can`t undestend whos to blame.

      i create test project to easy reproduce this case - https://github.com/evg345/spring-data-hibernate-bug

      @Entity
      public class Prima implements Serializable {
      
          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
          Long id;
      
          //@PrimaryKeyJoinColumn
          @OneToOne(mappedBy = "parent", optional = true , cascade = CascadeType.ALL)
          Secunda optionalData;
      }
      
      @Entity
      public class Secunda implements Serializable {
      
          @Id
          @Column(name = "id", nullable = false)
          Long id;
      
          @MapsId
          @OneToOne(optional = false)
          @JoinColumn(name = "id", nullable = false)
          Prima parent;
      }
      
      class Test {
         Long primaId;
         void beforTest() {
              Prima prim = new Prima();
              prim.setOptionalData(null); // <-- null
              prim = repoPrima.save(prim);
             this.primaId = prim.getId();
         }
      
         void test() {
              // we emulate object recieved from json
              Prima prim = new Prima();
              prim.setId(this.primaId);
              {
                  Secunda sec = new Secunda();
                  sec.setParent(prim); // <-- not null ! ! !
                  prim.setOptionalData(sec);
              }
      
              Prima savedPrima = repoPrima.save(prim); // <-- exception here
        }
      }
      

      Once again, this code works with Hibernate 5.2.12 , 5.2.13
      and fail when change Hibernate version in pom file to 5.2.14 , 5.2.16

      I expect that is bug somewhere inside Spring-Data , but it can hides in Hibernate

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                olivergierke Oliver Drotbohm
                Reporter:
                w2k Chursin Evgeny
                Last updater:
                Vanitha R
              • Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: