From d651714fd926e534c451c20b9b1ada40531ea6a5 Mon Sep 17 00:00:00 2001 From: Fabio Massimo Ercoli Date: Wed, 30 Sep 2020 17:19:07 +0200 Subject: [PATCH 1/2] HHH-14241 Test ImplicitNamingStrategyComponentPathImpl with IdClass --- .../id/idclass/IdClassNamingStrategyTest.java | 48 +++++++++++++++ .../org/hibernate/id/idclass/MyEntity.java | 41 +++++++++++++ .../org/hibernate/id/idclass/MyEntityId.java | 58 +++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/id/idclass/IdClassNamingStrategyTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/id/idclass/MyEntity.java create mode 100644 hibernate-core/src/test/java/org/hibernate/id/idclass/MyEntityId.java diff --git a/hibernate-core/src/test/java/org/hibernate/id/idclass/IdClassNamingStrategyTest.java b/hibernate-core/src/test/java/org/hibernate/id/idclass/IdClassNamingStrategyTest.java new file mode 100644 index 000000000000..dd815e1f893a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/id/idclass/IdClassNamingStrategyTest.java @@ -0,0 +1,48 @@ +/* + * 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 . + */ +package org.hibernate.id.idclass; + +import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl; +import org.hibernate.cfg.Configuration; + +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +public class IdClassNamingStrategyTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { MyEntity.class }; + } + + @Override + protected void configure(Configuration configuration) { + /* + * With this implicit naming strategy, we got the following mapping: + * + * create table MyEntity ( + * id_idA bigint not null, + * id_idB bigint not null, + * _identifierMapper_idA bigint not null, <-- ?? + * _identifierMapper_idB bigint not null, <-- ?? + * notes varchar(255), + * primary key (id_idA, id_idB) + * ) + */ + configuration.setImplicitNamingStrategy( new ImplicitNamingStrategyComponentPathImpl() ); + } + + @Test + public void test() { + inTransaction( ( session ) -> { + MyEntity entity = new MyEntity(); + entity.setId( new MyEntityId( 739L, 777L ) ); + + session.persist( entity ); + } ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/id/idclass/MyEntity.java b/hibernate-core/src/test/java/org/hibernate/id/idclass/MyEntity.java new file mode 100644 index 000000000000..610f9084c4ed --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/id/idclass/MyEntity.java @@ -0,0 +1,41 @@ +/* + * 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 . + */ +package org.hibernate.id.idclass; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; + +@Entity +@IdClass( MyEntityId.class ) +public class MyEntity { + + @Id + private Long idA; + + @Id + private Long idB; + + private String notes; + + public MyEntityId getId() { + return new MyEntityId( idB, idA ); + } + + public void setId(MyEntityId id) { + this.idB = id.getIdA(); + this.idA = id.getIdB(); + } + + public String getNotes() { + return notes; + } + + public void setNotes(String notes) { + this.notes = notes; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/id/idclass/MyEntityId.java b/hibernate-core/src/test/java/org/hibernate/id/idclass/MyEntityId.java new file mode 100644 index 000000000000..4915db393c54 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/id/idclass/MyEntityId.java @@ -0,0 +1,58 @@ +/* + * 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 . + */ +package org.hibernate.id.idclass; + +import java.io.Serializable; +import java.util.Objects; + +public class MyEntityId implements Serializable { + + private Long idA; + private Long idB; + + public MyEntityId(Long generatedId, Long providedId) { + this.idA = generatedId; + this.idB = providedId; + } + + private MyEntityId() { + } + + public Long getIdA() { + return idA; + } + + public void setIdA(Long idA) { + this.idA = idA; + } + + public Long getIdB() { + return idB; + } + + public void setIdB(Long idB) { + this.idB = idB; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + MyEntityId pk = (MyEntityId) o; + return Objects.equals( idA, pk.idA ) && + Objects.equals( idB, pk.idB ); + } + + @Override + public int hashCode() { + return Objects.hash( idA, idB ); + } +} From 30a0d73c5e88d47301228acf2fe297ae4db32c28 Mon Sep 17 00:00:00 2001 From: Jan Schatteman Date: Mon, 5 Oct 2020 21:33:46 +0200 Subject: [PATCH 2/2] HHH-14241 Support ImplicitNamingStrategyComponentPathImpl with IdClass Signed-off-by: Jan Schatteman --- ...mplicitNamingStrategyComponentPathImpl.java | 18 +++++++++++------- .../id/idclass/IdClassNamingStrategyTest.java | 12 +++++++----- .../org/hibernate/id/idclass/MyEntity.java | 6 +++--- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/naming/ImplicitNamingStrategyComponentPathImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/model/naming/ImplicitNamingStrategyComponentPathImpl.java index acfccfb49e77..42fc00b2c6c8 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/naming/ImplicitNamingStrategyComponentPathImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/naming/ImplicitNamingStrategyComponentPathImpl.java @@ -8,6 +8,7 @@ import org.hibernate.boot.model.source.spi.AttributePath; import org.hibernate.internal.util.StringHelper; +import org.hibernate.loader.PropertyPath; /** * An ImplicitNamingStrategy implementation which uses full composite paths @@ -30,14 +31,17 @@ protected String transformAttributePath(AttributePath attributePath) { } public static void process(AttributePath attributePath, StringBuilder sb) { - if ( attributePath.getParent() != null ) { - process( attributePath.getParent(), sb ); - if ( StringHelper.isNotEmpty( attributePath.getParent().getProperty() ) ) { - sb.append( '_' ); - } - } - String property = attributePath.getProperty(); + final AttributePath parent = attributePath.getParent(); + if ( parent != null && StringHelper.isNotEmpty( parent.getProperty() ) ) { + process( parent, sb ); + sb.append( '_' ); + } + else if ( PropertyPath.IDENTIFIER_MAPPER_PROPERTY.equals( property ) ) { + // skip it, do not pass go + sb.append( "id" ); + return; + } property = property.replace( "<", "" ); property = property.replace( ">", "" ); diff --git a/hibernate-core/src/test/java/org/hibernate/id/idclass/IdClassNamingStrategyTest.java b/hibernate-core/src/test/java/org/hibernate/id/idclass/IdClassNamingStrategyTest.java index dd815e1f893a..ca3ca493b331 100644 --- a/hibernate-core/src/test/java/org/hibernate/id/idclass/IdClassNamingStrategyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/id/idclass/IdClassNamingStrategyTest.java @@ -9,6 +9,7 @@ import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl; import org.hibernate.cfg.Configuration; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Test; @@ -21,10 +22,10 @@ protected Class[] getAnnotatedClasses() { @Override protected void configure(Configuration configuration) { - /* - * With this implicit naming strategy, we got the following mapping: - * - * create table MyEntity ( + /* + * With this implicit naming strategy, we got the following mapping: + * + * create table MyEntity ( * id_idA bigint not null, * id_idB bigint not null, * _identifierMapper_idA bigint not null, <-- ?? @@ -32,11 +33,12 @@ protected void configure(Configuration configuration) { * notes varchar(255), * primary key (id_idA, id_idB) * ) - */ + */ configuration.setImplicitNamingStrategy( new ImplicitNamingStrategyComponentPathImpl() ); } @Test + @TestForIssue(jiraKey = "HHH-14241") public void test() { inTransaction( ( session ) -> { MyEntity entity = new MyEntity(); diff --git a/hibernate-core/src/test/java/org/hibernate/id/idclass/MyEntity.java b/hibernate-core/src/test/java/org/hibernate/id/idclass/MyEntity.java index 610f9084c4ed..924b898ce160 100644 --- a/hibernate-core/src/test/java/org/hibernate/id/idclass/MyEntity.java +++ b/hibernate-core/src/test/java/org/hibernate/id/idclass/MyEntity.java @@ -23,12 +23,12 @@ public class MyEntity { private String notes; public MyEntityId getId() { - return new MyEntityId( idB, idA ); + return new MyEntityId( idA, idB ); } public void setId(MyEntityId id) { - this.idB = id.getIdA(); - this.idA = id.getIdB(); + this.idA = id.getIdA(); + this.idB = id.getIdB(); } public String getNotes() {