Merge remote-tracking branch 'upstream/master' into wip/6.0

This commit is contained in:
Andrea Boriero 2020-10-12 09:15:56 +01:00
commit 776a0402f6
4 changed files with 160 additions and 7 deletions

View File

@ -8,6 +8,7 @@ package org.hibernate.boot.model.naming;
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 @@ public class ImplicitNamingStrategyComponentPathImpl extends ImplicitNamingStrat
}
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( ">", "" );

View File

@ -0,0 +1,50 @@
/*
* 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.TestForIssue;
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
@TestForIssue(jiraKey = "HHH-14241")
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( idA, idB );
}
public void setId(MyEntityId id) {
this.idA = id.getIdA();
this.idB = 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 );
}
}