From c558fb7a5caa3557b1e9e2de7c77eff4bf608f06 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 30 Oct 2023 10:12:06 +0100 Subject: [PATCH] HHH-15074 Support for partially-generated `@EmbeddedId`s --- .../boot/model/internal/EmbeddableBinder.java | 7 +- .../EmbeddedIdGeneratedValueTest.java | 104 ++++++++++++++++++ .../RecordEmbeddedIdGeneratedValueTest.java | 82 ++++++++++++++ 3 files changed, 187 insertions(+), 6 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/identifier/EmbeddedIdGeneratedValueTest.java create mode 100644 hibernate-core/src/test/java17/org/hibernate/orm/test/records/RecordEmbeddedIdGeneratedValueTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java index 63d8f9df93..8781b94be9 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java @@ -387,7 +387,7 @@ public class EmbeddableBinder { ); final XProperty property = propertyAnnotatedElement.getProperty(); - if ( isGeneratedId( property ) ) { + if ( property.isAnnotationPresent( GeneratedValue.class ) ) { processGeneratedId( context, component, property ); } } @@ -467,11 +467,6 @@ public class EmbeddableBinder { } } - private static boolean isGeneratedId(XProperty property) { - return property.isAnnotationPresent( GeneratedValue.class ) - && property.isAnnotationPresent( Id.class ); - } - private static void processCompositeUserType(Component component, CompositeUserType compositeUserType) { component.sortProperties(); final List sortedPropertyNames = new ArrayList<>( component.getPropertySpan() ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/identifier/EmbeddedIdGeneratedValueTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/identifier/EmbeddedIdGeneratedValueTest.java new file mode 100644 index 0000000000..d185d7e80e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/identifier/EmbeddedIdGeneratedValueTest.java @@ -0,0 +1,104 @@ +/* + * 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.identifier; + +import java.io.Serializable; + +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.Test; + +import jakarta.persistence.Embeddable; +import jakarta.persistence.EmbeddedId; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Marco Belladelli + */ +@DomainModel( annotatedClasses = { + EmbeddedIdGeneratedValueTest.SystemUser.class, + EmbeddedIdGeneratedValueTest.PK.class, +} ) +@SessionFactory +@Jira( "https://hibernate.atlassian.net/browse/HHH-15074" ) +public class EmbeddedIdGeneratedValueTest { + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createMutationQuery( "delete from SystemUser" ).executeUpdate() ); + } + + @Test + public void test(SessionFactoryScope scope) { + final SystemUser _systemUser = scope.fromTransaction( session -> { + final SystemUser systemUser = new SystemUser(); + systemUser.setId( new PK( "mbladel" ) ); + systemUser.setName( "Marco Belladelli" ); + session.persist( systemUser ); + return systemUser; + } ); + + scope.inSession( session -> { + final SystemUser systemUser = session.find( SystemUser.class, _systemUser.getId() ); + assertThat( systemUser.getName() ).isEqualTo( "Marco Belladelli" ); + assertThat( systemUser.getId().getUsername() ).isEqualTo( "mbladel" ); + assertThat( systemUser.getId().getRegistrationId() ).isNotNull(); + } ); + } + + @Entity( name = "SystemUser" ) + public static class SystemUser { + @EmbeddedId + private PK id; + + private String name; + + public PK getId() { + return id; + } + + public void setId(PK id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @Embeddable + public static class PK implements Serializable { + private String username; + + @GeneratedValue + private Integer registrationId; + + public PK() { + } + + public PK(String username) { + this.username = username; + } + + public String getUsername() { + return username; + } + + public Integer getRegistrationId() { + return registrationId; + } + } +} diff --git a/hibernate-core/src/test/java17/org/hibernate/orm/test/records/RecordEmbeddedIdGeneratedValueTest.java b/hibernate-core/src/test/java17/org/hibernate/orm/test/records/RecordEmbeddedIdGeneratedValueTest.java new file mode 100644 index 0000000000..e276b45851 --- /dev/null +++ b/hibernate-core/src/test/java17/org/hibernate/orm/test/records/RecordEmbeddedIdGeneratedValueTest.java @@ -0,0 +1,82 @@ +/* + * 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.records; + +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.Test; + +import jakarta.persistence.Embeddable; +import jakarta.persistence.EmbeddedId; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Marco Belladelli + */ +@DomainModel( annotatedClasses = { + RecordEmbeddedIdGeneratedValueTest.SystemUser.class, + RecordEmbeddedIdGeneratedValueTest.PK.class, +} ) +@SessionFactory +@Jira( "https://hibernate.atlassian.net/browse/HHH-15074" ) +public class RecordEmbeddedIdGeneratedValueTest { + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createMutationQuery( "delete from SystemUser" ).executeUpdate() ); + } + + @Test + public void test(SessionFactoryScope scope) { + final SystemUser _systemUser = scope.fromTransaction( session -> { + final SystemUser systemUser = new SystemUser(); + systemUser.setId( new PK( "mbladel", null ) ); + systemUser.setName( "Marco Belladelli" ); + session.persist( systemUser ); + return systemUser; + } ); + + scope.inSession( session -> { + final SystemUser systemUser = session.find( SystemUser.class, _systemUser.getId() ); + assertThat( systemUser.getName() ).isEqualTo( "Marco Belladelli" ); + assertThat( systemUser.getId().username() ).isEqualTo( "mbladel" ); + assertThat( systemUser.getId().registrationId() ).isNotNull(); + } ); + } + + @Entity( name = "SystemUser" ) + public static class SystemUser { + @EmbeddedId + private PK id; + + private String name; + + public PK getId() { + return id; + } + + public void setId(PK id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @Embeddable + public record PK(String username, @GeneratedValue Integer registrationId) {} +}