HHH-15074 Support for partially-generated `@EmbeddedId`s

This commit is contained in:
Marco Belladelli 2023-10-30 10:12:06 +01:00
parent c700dcd8b6
commit c558fb7a5c
3 changed files with 187 additions and 6 deletions

View File

@ -387,7 +387,7 @@ public class EmbeddableBinder {
); );
final XProperty property = propertyAnnotatedElement.getProperty(); final XProperty property = propertyAnnotatedElement.getProperty();
if ( isGeneratedId( property ) ) { if ( property.isAnnotationPresent( GeneratedValue.class ) ) {
processGeneratedId( context, component, property ); 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) { private static void processCompositeUserType(Component component, CompositeUserType<?> compositeUserType) {
component.sortProperties(); component.sortProperties();
final List<String> sortedPropertyNames = new ArrayList<>( component.getPropertySpan() ); final List<String> sortedPropertyNames = new ArrayList<>( component.getPropertySpan() );

View File

@ -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;
}
}
}

View File

@ -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) {}
}