8 Replies Latest reply on Mar 6, 2009 9:15 AM by Joe Schemenauer

    GetReference vs find audit difference?

    Joe Schemenauer Newbie

      Hello

      Thanks for the great work on this project.

      I am experiencing an anomaly when working with a @ManyToOne mapping. I am a newbie when it comes to hibernate, Envers and JPA so I may very well be doing something wrong.

      I have a PersonName entity and a PersonNameType entity with a unidirectional @ManyToOne mapping from the PersonName to the PersonNameType.

      I attempt to persist a PersonName Entity with the PersonNameType set.

      The main table PersonName sets the personNameTypeId field correctly in both cases when either the find or getReference methods are used to load the PersonNameType object.

      The Audit table record for the insert only gets the personNameTypeId field set correctly when using the find method to load the PersonNameType object.

      When the getReference is used to load the PersonNameType entity the Audit record will contain NULL for the personNameTypeId field.

      Relevant PersonName enity unidirectional @ManyToOne mapping to PersonNameType.

       @ManyToOne(fetch = FetchType.LAZY)
       @JoinColumn(name = "personNameTypeId", nullable = false)
       @NotNull
       private PersonNameType personNameType;
      


      Test Program.
       EntityManagerFactory emf = Persistence.createEntityManagerFactory("edm");
      
       EntityManager em = emf.createEntityManager();
       EntityTransaction tx = em.getTransaction();
       tx.begin();
      
       //this line will work (find)
       //PersonNameType personNameType = em.find(PersonNameType.class, new Long(4));
      
       //This line does not store the id in the audit table (getReference)
       PersonNameType personNameType = em.getReference(PersonNameType.class, new Long(4));
      
       PersonName personName = new PersonName();
       personName.setLastName("lastName");
       personName.setFirstName("firstName");
       personName.setMiddleName("middleName");
       personName.setSuffix("");
       personName.setPersonNameType(personNameType);
       em.persist(personName);
      
      
       tx.commit();
       em.close();
       emf.close();