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 0000000000..dd815e1f89 --- /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 0000000000..610f9084c4 --- /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 0000000000..4915db393c --- /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 ); + } +}