[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-7304) NaturalIdResolutionCache not correctly filled on first persist when @GeneratedValue- at Id
Guenther Demetz (JIRA)
noreply at atlassian.com
Wed May 9 11:52:09 EDT 2012
[ https://hibernate.onjira.com/browse/HHH-7304?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=46581#comment-46581 ]
Guenther Demetz commented on HHH-7304:
--------------------------------------
N.B.: The proposed pull-request will also prevent the mentioned unneccessary find-by-natural-id-query
> 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
> Labels: natural-id, performance
>
> {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