Wrong data in self-reference using L2C and BatchSize
Description
Attachments
1
- 24 Feb 2023, 01:10 PM
Activity
Show:
Steve Ebersole
changed the StatusMarch 1, 2023 at 8:33 PMResolved
Closed

Christian Beikov
changed the StatusMarch 1, 2023 at 10:10 AMOpen
Resolved

Christian Beikov
updated the ResolutionMarch 1, 2023 at 10:10 AMNone
Fixed

Andrea Boriero
updated the Fix versionsFebruary 28, 2023 at 11:26 AMNone
6.2.0

Andrea Boriero
changed the AssigneeFebruary 28, 2023 at 10:11 AMUnassigned

Andrea Boriero

MK
created the IssueFebruary 24, 2023 at 1:18 PMFixed
Details
Details
Assignee
Andrea Boriero
Andrea BorieroReporter
MK
MKWorked in
Components
Fix versions
Affects versions
Priority
Created February 24, 2023 at 1:18 PM
Updated March 1, 2023 at 8:33 PM
Resolved March 1, 2023 at 10:10 AM
When loading entities that have a field referencing itself (representing a parent-child-relationship), the field may be filled with wrong data if the second-level cache is used along with BatchSize.
In the example below (it is also attached), an entity which does not have a parent category set is loaded with a parent category. The last assertion fails in 6.2.0.CR2 but passes in 6.1.7.Final, as it should.
Entity:
@NamedQueries( { @NamedQuery(name = "Category.selectAll", query = " SELECT c" + " FROM Category c" + " ORDER BY c.name") }) @Entity @BatchSize(size = 500) @Cacheable @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class Category { @Id @GeneratedValue private Long id; @Column private String name; @ManyToOne @JoinColumn @Fetch(value = FetchMode.SELECT) private Category parentCategory; }
Test:
@Test public void hhh123Test() throws Exception { createEntities(); EntityManager entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); List<Category> categories = entityManager.createNamedQuery("Category.selectAll", Category.class).getResultList(); entityManager.getTransaction().commit(); entityManager.close(); Assert.assertNull(categories.get(0).getParentCategory()); Assert.assertEquals("A", categories.get(1).getParentCategory().getName()); Assert.assertNull(categories.get(2).getParentCategory()); // This assertion fails, even though it should pass } private void createEntities() { EntityManager entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); Category categoryA = new Category(); categoryA.setName("A"); entityManager.persist(categoryA); Category categoryB = new Category(); categoryB.setName("B"); categoryB.setParentCategory(categoryA); entityManager.persist(categoryB); Category categoryC = new Category(); categoryC.setName("C"); entityManager.persist(categoryC); entityManager.getTransaction().commit(); entityManager.close(); }