HHH-14241 Test ImplicitNamingStrategyComponentPathImpl with IdClass

This commit is contained in:
Fabio Massimo Ercoli 2020-09-30 17:19:07 +02:00 committed by Jan Schatteman
parent 3a88b1c6b2
commit 03416a8cdd
3 changed files with 147 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 );
} );
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 );
}
}