From aa7c67691def6794de8debc36b08b9187c57091e Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Wed, 22 Mar 2023 17:45:33 +0100 Subject: [PATCH] HHH-16355 Add test for issue --- .../OneToManyMapEntityKeyMappedByTest.java | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/mappedBy/OneToManyMapEntityKeyMappedByTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/mappedBy/OneToManyMapEntityKeyMappedByTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/mappedBy/OneToManyMapEntityKeyMappedByTest.java new file mode 100644 index 0000000000..a12c2fddfb --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/mappedBy/OneToManyMapEntityKeyMappedByTest.java @@ -0,0 +1,140 @@ +/* + * 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.mappedBy; + +import java.util.HashMap; +import java.util.Map; + +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.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; + +import static org.assertj.core.api.Assertions.assertThat; + +@SessionFactory +@DomainModel( annotatedClasses = { + OneToManyMapEntityKeyMappedByTest.MapContainerEntity.class, + OneToManyMapEntityKeyMappedByTest.MapKeyEntity.class, + OneToManyMapEntityKeyMappedByTest.MapValueEntity.class +} ) +@Jira( "https://hibernate.atlassian.net/browse/HHH-16355" ) +public class OneToManyMapEntityKeyMappedByTest { + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final MapContainerEntity container = new MapContainerEntity( 1L ); + final MapKeyEntity key = new MapKeyEntity( 2L ); + final MapValueEntity value = new MapValueEntity( 3L, container ); + container.getMap().put( key, value ); + session.persist( container ); + session.persist( key ); + session.persist( value ); + } ); + } + + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.createMutationQuery( "delete from MapValueEntity" ).executeUpdate(); + session.createMutationQuery( "delete from MapKeyEntity" ).executeUpdate(); + session.createMutationQuery( "delete from MapContainerEntity" ).executeUpdate(); + } ); + } + + @Test + public void test(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final MapContainerEntity container = session.createQuery( + "from MapContainerEntity", + MapContainerEntity.class + ).getSingleResult(); + assertThat( container.getId() ).isEqualTo( 1L ); + assertThat( container.getMap() ).hasSize( 1 ); + final Map.Entry entry = container.getMap().entrySet().iterator().next(); + assertThat( entry.getKey().getId() ).isEqualTo( 2L ); + assertThat( entry.getValue().getId() ).isEqualTo( 3L ); + } ); + } + + @Entity( name = "MapContainerEntity" ) + @Table( name = "map_container_entity" ) + public static class MapContainerEntity { + @Id + private Long id; + @OneToMany( mappedBy = "container" ) + private Map map; + + public MapContainerEntity() { + } + + public MapContainerEntity(Long id) { + this.id = id; + this.map = new HashMap<>(); + } + + public Long getId() { + return id; + } + + public Map getMap() { + return map; + } + } + + @Entity( name = "MapKeyEntity" ) + @Table( name = "map_key_entity" ) + public static class MapKeyEntity { + @Id + private Long id; + + public MapKeyEntity() { + } + + public MapKeyEntity(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + } + + @Entity( name = "MapValueEntity" ) + @Table( name = "map_value_entity" ) + public static class MapValueEntity { + @Id + private Long id; + @ManyToOne + private MapContainerEntity container; + + public MapValueEntity() { + } + + public MapValueEntity(Long id, MapContainerEntity container) { + this.id = id; + this.container = container; + } + + public Long getId() { + return id; + } + + public MapContainerEntity getContainer() { + return container; + } + } +}