We're updating the issue view to help you get more done. 

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

Description

Employee.java

1 2 3 4 5 6 7 @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; }

NaturalIdTest1.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 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);

NaturalIdTest2.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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);

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.

Environment

None

Status

Assignee

Brett Meyer

Reporter

Frank Schwarz

Components

Fix versions

Affects versions

4.1.3

Priority

Major