MK (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiYWU4NjMxY2M4...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16230?atlOrigin=eyJpIjoiYWU4Nj...
) HHH-16230 (
https://hibernate.atlassian.net/browse/HHH-16230?atlOrigin=eyJpIjoiYWU4Nj...
) Wrong data in self-reference using L2C and BatchSize (
https://hibernate.atlassian.net/browse/HHH-16230?atlOrigin=eyJpIjoiYWU4Nj...
)
Issue Type: Bug Affects Versions: 6.2.0.CR2 Assignee: Unassigned Attachments:
wrong-data-in-self-reference.zip Components: hibernate-core Created: 24/Feb/2023 05:18 AM
Environment: Worked in 6.1.7.Final, regression in 6.2.0.CR2. Priority: Blocker Reporter:
MK (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3...
)
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();
}
(
https://hibernate.atlassian.net/browse/HHH-16230#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16230#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100217- sha1:077879e )