[hibernate-issues] [Hibernate-JIRA] Created: (HHH-7304) NaturalIdResolutionCache not correctly filled on first persist when @GeneratedValue- at Id

Frank Schwarz (JIRA) noreply at atlassian.com
Sun May 6 18:07:11 EDT 2012


NaturalIdResolutionCache not correctly filled on first persist when @GeneratedValue- at Id
---------------------------------------------------------------------------------------

                 Key: HHH-7304
                 URL: https://hibernate.onjira.com/browse/HHH-7304
             Project: Hibernate ORM
          Issue Type: Bug
          Components: core
    Affects Versions: 4.1.3
            Reporter: Frank Schwarz


{code:title=Employee.java|borderStyle=solid}
@Entity
public class Employee{
  @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id;
  @NaturalId(mutable = true) private String name;
  @NaturalId(mutable = true) private String surname;
  private String position;
}
{code}

{code:title=NaturalIdTest1.java|borderStyle=solid}
entityManager.getTransaction().begin();
Employee e = new Employee();
e.setSurname("Hans");
e.setName("Dampf");
e.setPosition("Junior");
entityManager.persist(e);
entityManager.getTransaction().commit();

Session session = entityManager.unwrap(Session.class);
Employee e2 = (Employee) session.byNaturalId(Employee.class)
    .using("surname", "Hans")
    .using("name", "Dampf")
    .load();
System.out.println(e2);
{code}

{code:title=NaturalIdTest2.java|borderStyle=solid}
entityManager.getTransaction().begin();
Employee e = new Employee();
e.setSurname("Hans");
e.setName("Dampf");
e.setPosition("Junior");
entityManager.persist(e);

// this is the only difference
e.setPosition("Senior");

entityManager.getTransaction().commit();

Session session = entityManager.unwrap(Session.class);
Employee e2 = (Employee) session.byNaturalId(Employee.class)
    .using("surname", "Hans")
    .using("name", "Dampf")
    .load();
System.out.println(e2);
{code}

The NaturalIdTest1 will issue an SQL statement for the find-by-natural-id-query; the NaturalIdTest2 does not.

Put a breakpoint into NaturalIdResolutionCache#cache(Serializable, Object[]) and watch the pk-parameter to be {{null}} on the first invocation and not {{null}} on the second invocation.

If the @Id-field is not @GeneratedValue, the first invocation of NaturalIdResolutionCache#cache will be already correct.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list