From 89888456d02b2a9827eaf6052adc31918d6253f8 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Fri, 25 Aug 2023 10:34:21 +0200 Subject: [PATCH] HHH-17105 Add test for issue --- ...ManyToManySQLJoinTableRestrictionTest.java | 247 ++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/manytomany/ManyToManySQLJoinTableRestrictionTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/manytomany/ManyToManySQLJoinTableRestrictionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/manytomany/ManyToManySQLJoinTableRestrictionTest.java new file mode 100644 index 0000000000..dd6c511ec7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/manytomany/ManyToManySQLJoinTableRestrictionTest.java @@ -0,0 +1,247 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.mapping.manytomany; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.hibernate.annotations.SQLInsert; +import org.hibernate.annotations.SQLJoinTableRestriction; + +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinTable; +import jakarta.persistence.ManyToMany; +import jakarta.persistence.OrderColumn; +import jakarta.persistence.Table; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Marco Belladelli + */ +@DomainModel( annotatedClasses = { + ManyToManySQLJoinTableRestrictionTest.Project.class, + ManyToManySQLJoinTableRestrictionTest.User.class, + ManyToManySQLJoinTableRestrictionTest.ProjectUsers.class, +} ) +@SessionFactory( useCollectingStatementInspector = true ) +@Jira( "https://hibernate.atlassian.net/browse/HHH-17105" ) +public class ManyToManySQLJoinTableRestrictionTest { + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final User user1 = new User( "user1" ); + final Project project1 = new Project( "p1" ); + project1.getManagers().add( user1 ); + project1.getMembers().add( user1 ); + final Project project2 = new Project( "p2" ); + project2.getMembers().add( user1 ); + session.persist( user1 ); + session.persist( project1 ); + session.persist( project2 ); + final User user2 = new User( "user2" ); + final User user3 = new User( "user3" ); + final Project project3 = new Project( "p3" ); + project3.getMembers().add( user2 ); + project3.getMembers().add( user3 ); + project3.getManagers().add( user2 ); + project3.getOrderedUsers().add(user3); + project3.getOrderedUsers().add(user2); + session.persist( user2 ); + session.persist( user3 ); + session.persist( project3 ); + } ); + } + + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.createMutationQuery( "delete from Project" ).executeUpdate(); + session.createMutationQuery( "delete from User" ).executeUpdate(); + } ); + } + + @Test + public void testJoinTableRemoveEmptyCollection(SessionFactoryScope scope) { + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); + scope.inTransaction( session -> { + final Project p1 = session.find( Project.class, "p1" ); + p1.getManagers().remove( p1.getManagers().iterator().next() ); + assertThat( p1.getManagers() ).isEmpty(); + inspector.clear(); + } ); + assertThat( inspector.getSqlQueries() ).hasSize( 1 ); + assertThat( inspector.getSqlQueries().get( 0 ) ).contains( "manager" ); + scope.inTransaction( session -> { + final User user1 = session.find( User.class, "user1" ); + assertThat( user1.getManagedProjects() ).isEmpty(); + assertThat( user1.getOtherProjects().stream().map( Project::getName ) ).contains( "p1", "p2" ); + } ); + } + + @Test + public void testJoinTableRemoveNonEmptyCollection(SessionFactoryScope scope) { + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); + scope.inTransaction( session -> { + final User user = session.find( User.class, "user2" ); + final Project p3 = session.find( Project.class, "p3" ); + p3.getMembers().remove( user ); + assertThat( p3.getMembers() ).isNotEmpty(); + inspector.clear(); + } ); + assertThat( inspector.getSqlQueries() ).hasSize( 1 ); + assertThat( inspector.getSqlQueries().get( 0 ) ).contains( "member" ); + scope.inTransaction( session -> { + final User user2 = session.find( User.class, "user2" ); + assertThat( user2.getOtherProjects() ).isEmpty(); + assertThat( user2.getManagedProjects().stream().map( Project::getName ) ).contains( "p3" ); + } ); + } + + @Test + public void testJoinTableUpdate(SessionFactoryScope scope) { + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); + scope.inTransaction( session -> { + final Project p3 = session.find( Project.class, "p3" ); + assertThat( p3.getOrderedUsers().stream().map( User::getName ) ).containsExactly( "user3", "user2" ); + p3.getOrderedUsers().sort( Comparator.comparing( User::getName ) ); + inspector.clear(); + } ); + assertThat( inspector.getSqlQueries() ).hasSize( 2 ); + assertThat( inspector.getSqlQueries().get( 0 ) ).contains( "order_col is not null" ); + assertThat( inspector.getSqlQueries().get( 1 ) ).contains( "order_col is not null" ); + scope.inTransaction( session -> { + final Project p3 = session.find( Project.class, "p3" ); + assertThat( p3.getOrderedUsers().stream().map( User::getName ) ).containsExactly( "user2", "user3" ); + } ); + } + + @Entity( name = "Project" ) + @Table( name = "t_project" ) + public static class Project { + @Id + private String name; + + @ManyToMany + @JoinTable( + name = "project_users", + joinColumns = { @JoinColumn( name = "project_id" ) }, + inverseJoinColumns = { @JoinColumn( name = "user_id" ) } + ) + @SQLJoinTableRestriction( "role = 'manager'" ) + @SQLInsert( sql = "insert into project_users (project_id, user_id, role) values (?, ?, 'manager')" ) + private Set managers = new HashSet<>(); + + @ManyToMany + @JoinTable( + name = "project_users", + joinColumns = { @JoinColumn( name = "project_id" ) }, + inverseJoinColumns = { @JoinColumn( name = "user_id" ) } + ) + @SQLJoinTableRestriction( "role = 'member'" ) + @SQLInsert( sql = "insert into project_users (project_id, user_id, role) values (?, ?, 'member')" ) + private Set members = new HashSet<>(); + + @ManyToMany + @JoinTable( + name = "ordered_users", + joinColumns = { @JoinColumn( name = "project_id" ) }, + inverseJoinColumns = { @JoinColumn( name = "user_id" ) } + ) + @SQLJoinTableRestriction( "order_col is not null" ) + @OrderColumn( name = "order_col" ) + private List orderedUsers = new ArrayList<>(); + + public Project() { + } + + public Project(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public Set getManagers() { + return managers; + } + + public Set getMembers() { + return members; + } + + public List getOrderedUsers() { + return orderedUsers; + } + } + + @Entity( name = "ProjectUsers" ) + @Table( name = "project_users" ) + public static class ProjectUsers { + @Id + @Column( name = "project_id" ) + private String projectId; + + @Id + @Column( name = "user_id" ) + private String userId; + + @Id + @Column( name = "role" ) + private String role; + } + + @Entity( name = "User" ) + @Table( name = "t_user" ) + public static class User { + @Id + private String name; + + @ManyToMany( mappedBy = "managers" ) + @SQLJoinTableRestriction( "role = 'manager'" ) + private Set managedProjects = new HashSet<>(); + + @ManyToMany( mappedBy = "members" ) + @SQLJoinTableRestriction( "role = 'member'" ) + private Set otherProjects = new HashSet<>(); + + public User() { + } + + public User(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public Set getManagedProjects() { + return managedProjects; + } + + public Set getOtherProjects() { + return otherProjects; + } + } +}