|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.interceptor; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.FetchType; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.JoinColumn; |
| 11 | +import jakarta.persistence.JoinTable; |
| 12 | +import jakarta.persistence.ManyToMany; |
| 13 | +import org.hibernate.CallbackException; |
| 14 | +import org.hibernate.Interceptor; |
| 15 | +import org.hibernate.collection.spi.PersistentCollection; |
| 16 | +import org.hibernate.engine.spi.SessionImplementor; |
| 17 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 18 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 19 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 20 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 21 | +import org.junit.jupiter.api.BeforeAll; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 30 | + |
| 31 | +@DomainModel( |
| 32 | + annotatedClasses = { |
| 33 | + CollectionRecreateInterceptorTest.Employee.class, |
| 34 | + CollectionRecreateInterceptorTest.Project.class |
| 35 | + } |
| 36 | +) |
| 37 | +@SessionFactory |
| 38 | +public class CollectionRecreateInterceptorTest { |
| 39 | + |
| 40 | + @BeforeAll |
| 41 | + public void setUp(SessionFactoryScope scope) { |
| 42 | + scope.inTransaction( |
| 43 | + session -> { |
| 44 | + Employee employee = new Employee( 1L ); |
| 45 | + Project project = new Project( 1L ); |
| 46 | + |
| 47 | + employee.projects.add( project ); |
| 48 | + project.employees.add( employee ); |
| 49 | + |
| 50 | + session.persist( project ); |
| 51 | + session.persist( employee ); |
| 52 | + } |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @JiraKey("HHH-3129") |
| 58 | + public void testInterceptorNpe(SessionFactoryScope scope) { |
| 59 | + scope.inTransaction( |
| 60 | + (SessionImplementor) scope.getSessionFactory().withOptions() |
| 61 | + .interceptor( new Interceptor() { |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onCollectionRecreate(Object collection, Object key) throws CallbackException { |
| 65 | + Interceptor.super.onCollectionRecreate( collection, key ); |
| 66 | + assertNotNull( ((PersistentCollection<?>) collection).getRole() ); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onCollectionUpdate(Object collection, Object key) throws CallbackException { |
| 71 | + Interceptor.super.onCollectionUpdate( collection, key ); |
| 72 | + assertNotNull( ((PersistentCollection<?>) collection).getRole() ); |
| 73 | + } |
| 74 | + } ) |
| 75 | + .openSession(), |
| 76 | + session -> { |
| 77 | + Employee employee = session.find( Employee.class, 1L ); |
| 78 | + Project newProject = new Project( 2L ); |
| 79 | + |
| 80 | + newProject.employees.add( employee ); |
| 81 | + employee.projects.add( newProject ); |
| 82 | + |
| 83 | + session.persist( newProject ); |
| 84 | + } |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + @Entity(name = "Employee") |
| 89 | + public static class Employee { |
| 90 | + @Id |
| 91 | + Long id; |
| 92 | + String name; |
| 93 | + |
| 94 | + @ManyToMany(mappedBy = "employees") |
| 95 | + Set<Project> projects = new HashSet<>(); |
| 96 | + |
| 97 | + public Employee() { |
| 98 | + } |
| 99 | + |
| 100 | + public Employee(Long id) { |
| 101 | + this.id = id; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Entity(name = "Project") |
| 106 | + public static class Project { |
| 107 | + @Id |
| 108 | + Long id; |
| 109 | + String name; |
| 110 | + |
| 111 | + @ManyToMany(fetch = FetchType.LAZY) |
| 112 | + @JoinTable( |
| 113 | + name = "employees_to_projects", |
| 114 | + joinColumns = @JoinColumn(name = "project_id", referencedColumnName = "id"), |
| 115 | + inverseJoinColumns = @JoinColumn(name = "employee_id") |
| 116 | + ) |
| 117 | + List<Employee> employees = new ArrayList<>(); |
| 118 | + |
| 119 | + public Project() { |
| 120 | + } |
| 121 | + |
| 122 | + public Project(Long id) { |
| 123 | + this.id = id; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments