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

This commit is contained in:
Andrea Boriero 2020-04-01 10:40:03 +01:00
commit 2c127bdbe3
6 changed files with 545 additions and 2957 deletions

File diff suppressed because it is too large Load Diff

View File

@ -333,12 +333,6 @@ public interface CoreMessageLogger extends BasicLogger {
@Message(value = "Found mapping document in jar: %s", id = 109)
void foundMappingDocument(String name);
@LogMessage(level = ERROR)
@Message(value = "Getters of lazy classes cannot be final: %s.%s", id = 112)
void gettersOfLazyClassesCannotBeFinal(
String entityName,
String name);
@LogMessage(level = WARN)
@Message(value = "GUID identifier generated: %s", id = 113)
void guidGenerated(String result);
@ -763,12 +757,6 @@ public interface CoreMessageLogger extends BasicLogger {
@Message(value = "Sessions opened: %s", id = 242)
void sessionsOpened(long sessionOpenCount);
@LogMessage(level = ERROR)
@Message(value = "Setters of lazy classes cannot be final: %s.%s", id = 243)
void settersOfLazyClassesCannotBeFinal(
String entityName,
String name);
@LogMessage(level = WARN)
@Message(value = "@Sort not allowed for an indexed collection, annotation ignored.", id = 244)
void sortAnnotationIndexedCollection();

View File

@ -11,6 +11,7 @@ import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
@ -84,14 +85,21 @@ public final class ProxyFactoryHelper {
Class clazz = persistentClass.getMappedClass();
while ( properties.hasNext() ) {
Property property = (Property) properties.next();
Method method = property.getGetter( clazz ).getMethod();
if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
LOG.gettersOfLazyClassesCannotBeFinal( persistentClass.getEntityName(), property.getName() );
}
method = property.getSetter( clazz ).getMethod();
if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
LOG.settersOfLazyClassesCannotBeFinal( persistentClass.getEntityName(), property.getName() );
}
validateGetterSetterMethodProxyability( "Getter", property.getGetter( clazz ).getMethod() );
validateGetterSetterMethodProxyability( "Setter", property.getSetter( clazz ).getMethod() );
}
}
public static void validateGetterSetterMethodProxyability(String getterOrSetter, Method method ) {
if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
throw new HibernateException(
String.format(
"%s methods of lazy classes cannot be final: %s#%s",
getterOrSetter,
method.getDeclaringClass().getName(),
method.getName()
)
);
}
}

View File

@ -88,13 +88,17 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer {
final Set<Class> proxyInterfaces = ProxyFactoryHelper.extractProxyInterfaces( persistentClass, entityName );
ProxyFactoryHelper.validateProxyability( persistentClass );
Method proxyGetIdentifierMethod = ProxyFactoryHelper.extractProxyGetIdentifierMethod( idGetter, proxyInterface );
Method proxySetIdentifierMethod = ProxyFactoryHelper.extractProxySetIdentifierMethod( idSetter, proxyInterface );
ProxyFactory pf = buildProxyFactoryInternal( persistentClass, idGetter, idSetter );
try {
ProxyFactoryHelper.validateGetterSetterMethodProxyability( "Getter", proxyGetIdentifierMethod );
ProxyFactoryHelper.validateGetterSetterMethodProxyability( "Setter", proxySetIdentifierMethod );
ProxyFactoryHelper.validateProxyability( persistentClass );
pf.postInstantiate(
entityName,
mappedClass,

View File

@ -13,8 +13,8 @@ import org.junit.Test;
import java.sql.SQLException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* Unit test of the {@link StandardDialectResolver} class.
@ -44,7 +44,7 @@ public class StandardDialectResolverTest extends BaseUnitTestCase {
@Test
public void testResolveDialectInternalForSQLServer2012()
throws SQLException {
runSQLServerDialectTest( 11, SQLServer2008Dialect.class );
runSQLServerDialectTest( 11, SQLServer2012Dialect.class );
}
@Test
@ -93,7 +93,7 @@ public class StandardDialectResolverTest extends BaseUnitTestCase {
@Test
public void testResolveDialectInternalForPostgres92() throws SQLException {
runPostgresDialectTest( 9, 2, PostgreSQL9Dialect.class );
runPostgresDialectTest( 9, 2, PostgreSQL92Dialect.class );
}
@Test
@ -131,6 +131,17 @@ public class StandardDialectResolverTest extends BaseUnitTestCase {
runMySQLDialectTest( 5, 7, MySQL57Dialect.class );
}
@Test
public void testResolveDialectInternalForMySQL6() throws SQLException {
runMySQLDialectTest( 6, 0, MySQL57Dialect.class );
}
@Test
public void testResolveDialectInternalForMySQL7() throws SQLException {
runMySQLDialectTest( 7, 0, MySQL57Dialect.class );
}
@Test
public void testResolveDialectInternalForMySQL8() throws SQLException {
runMySQLDialectTest( 8, 0, MySQL8Dialect.class );
@ -188,8 +199,11 @@ public class StandardDialectResolverTest extends BaseUnitTestCase {
String dbms = builder.toString();
assertNotNull( "Dialect for " + dbms + " should not be null", dialect );
assertTrue( "Dialect for " + dbms + " should be "
+ expectedDialect.getSimpleName(),
expectedDialect.isInstance( dialect ) );
// Make sure to test that the actual dialect class is as expected
// (not just an instance of the expected dialect.
assertEquals( "Dialect for " + dbms + " should be " + expectedDialect.getSimpleName(),
expectedDialect,
dialect.getClass()
);
}
}

View File

@ -0,0 +1,502 @@
/*
* 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.test.proxy;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
import org.hibernate.Hibernate;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Gail Badner
*/
@TestForIssue( jiraKey = "HHH-13891" )
public class FinalGetterSetterTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
public void testEntityWithFinalClass() {
doInHibernate( this::sessionFactory, session -> {
assertNull( session.get( EntityWithFinalClass.class, 999 ) );
});
try {
doInHibernate( this::sessionFactory, session -> {
session.load( EntityWithFinalClass.class, 999 );
});
fail( "Should have thrown ObjectNotFoundException" );
}
catch (ObjectNotFoundException expected) {
}
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalClass entity = new EntityWithFinalClass();
entity.id = 1;
entity.name = "An Entity";
session.persist( entity );
});
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalClass entity = session.load( EntityWithFinalClass.class, 1 );
assertNotNull( entity );
assertTrue( Hibernate.isInitialized( entity ) );
});
}
@Test
public void testEntityWithFinalIdGetter() {
doInHibernate( this::sessionFactory, session -> {
assertNull( session.get( EntityWithFinalIdGetter.class, 999 ) );
});
try {
doInHibernate( this::sessionFactory, session -> {
session.load( EntityWithFinalIdGetter.class, 999 );
});
fail( "Should have thrown ObjectNotFoundException" );
}
catch (ObjectNotFoundException expected) {
}
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalIdGetter entity = new EntityWithFinalIdGetter();
entity.id = 1;
entity.name = "An Entity";
session.persist( entity );
});
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalIdGetter entity = session.load( EntityWithFinalIdGetter.class, 1 );
assertNotNull( entity );
assertTrue( Hibernate.isInitialized( entity ) );
});
}
@Test
public void testEntityWithFinalIdSetter() {
doInHibernate( this::sessionFactory, session -> {
assertNull( session.get( EntityWithFinalIdSetter.class, 999 ) );
});
try {
doInHibernate( this::sessionFactory, session -> {
session.load( EntityWithFinalIdSetter.class, 999 );
});
fail( "Should have thrown ObjectNotFoundException" );
}
catch (ObjectNotFoundException expected) {
}
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalIdSetter entity = new EntityWithFinalIdSetter();
entity.id = 1;
entity.name = "An Entity";
session.persist( entity );
});
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalIdSetter entity = session.load( EntityWithFinalIdSetter.class, 1 );
assertNotNull( entity );
assertTrue( Hibernate.isInitialized( entity ) );
});
}
@Test
public void testEntityWithFinalVersionGetter() {
doInHibernate( this::sessionFactory, session -> {
assertNull( session.get( EntityWithFinalVersionGetter.class, 999 ) );
});
try {
doInHibernate( this::sessionFactory, session -> {
session.load( EntityWithFinalVersionGetter.class, 999 );
});
fail( "Should have thrown ObjectNotFoundException" );
}
catch (ObjectNotFoundException expected) {
}
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalVersionGetter entity = new EntityWithFinalVersionGetter();
entity.id = 1;
entity.name = "An Entity";
session.persist( entity );
});
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalVersionGetter entity = session.load( EntityWithFinalVersionGetter.class, 1 );
assertNotNull( entity );
assertTrue( Hibernate.isInitialized( entity ) );
});
}
@Test
public void testEntityWithFinalVersionSetter() {
doInHibernate( this::sessionFactory, session -> {
assertNull( session.get( EntityWithFinalVersionSetter.class, 999 ) );
});
try {
doInHibernate( this::sessionFactory, session -> {
session.load( EntityWithFinalVersionSetter.class, 999 );
});
fail( "Should have thrown ObjectNotFoundException" );
}
catch (ObjectNotFoundException expected) {
}
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalVersionSetter entity = new EntityWithFinalVersionSetter();
entity.id = 1;
entity.name = "An Entity";
session.persist( entity );
});
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalVersionSetter entity = session.load( EntityWithFinalVersionSetter.class, 1 );
assertNotNull( entity );
assertTrue( Hibernate.isInitialized( entity ) );
});
}
@Test
public void testEntityWithFinalPropertyGetter() {
doInHibernate( this::sessionFactory, session -> {
assertNull( session.get( EntityWithFinalPropertyGetter.class, 999 ) );
});
try {
doInHibernate( this::sessionFactory, session -> {
session.load( EntityWithFinalPropertyGetter.class, 999 );
});
fail( "Should have thrown ObjectNotFoundException" );
}
catch (ObjectNotFoundException expected) {
}
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalPropertyGetter entity = new EntityWithFinalPropertyGetter();
entity.id = 1;
entity.name = "An Entity";
session.persist( entity );
});
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalPropertyGetter entity = session.load( EntityWithFinalPropertyGetter.class, 1 );
assertNotNull( entity );
assertTrue( Hibernate.isInitialized( entity ) );
});
}
@Test
public void testEntityWithFinalPropertySetter() {
doInHibernate( this::sessionFactory, session -> {
assertNull( session.get( EntityWithFinalPropertySetter.class, 999 ) );
});
try {
doInHibernate( this::sessionFactory, session -> {
session.load( EntityWithFinalPropertySetter.class, 999 );
});
fail( "Should have thrown ObjectNotFoundException" );
}
catch (ObjectNotFoundException expected) {
}
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalPropertySetter entity = new EntityWithFinalPropertySetter();
entity.id = 1;
entity.name = "An Entity";
session.persist( entity );
});
doInHibernate( this::sessionFactory, session -> {
final EntityWithFinalPropertySetter entity = session.load( EntityWithFinalPropertySetter.class, 1 );
assertNotNull( entity );
assertTrue( Hibernate.isInitialized( entity ) );
});
}
@Override
public Class[] getAnnotatedClasses() {
return new Class[] {
EntityWithFinalClass.class,
EntityWithFinalIdGetter.class,
EntityWithFinalIdSetter.class,
EntityWithFinalVersionGetter.class,
EntityWithFinalVersionSetter.class,
EntityWithFinalPropertyGetter.class,
EntityWithFinalPropertySetter.class
};
}
@Entity( name = "EntityWithFinalClass")
public static final class EntityWithFinalClass {
@Id
private int id;
@Version
@Column(name = "ver")
private int version;
private String name;
public final int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity( name = "EntityWithFinalIdGetter")
public static class EntityWithFinalIdGetter {
@Id
private int id;
@Version
@Column(name = "ver")
private int version;
private String name;
public final int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity( name = "EntityWithFinalIdSetter")
public static class EntityWithFinalIdSetter {
@Id
private int id;
@Version
@Column(name = "ver")
private int version;
private String name;
public int getId() {
return id;
}
public final void setId(int id) {
this.id = id;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity( name = "EntityWithFinalVersionGetter")
public static class EntityWithFinalVersionGetter {
@Id
private int id;
@Version
@Column(name = "ver")
private int version;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public final int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity( name = "EntityWithFinalVersionSetter")
public static class EntityWithFinalVersionSetter {
@Id
private int id;
@Version
@Column(name = "ver")
private int version;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getVersion() {
return version;
}
public final void setVersion(int version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity( name = "EntityWithFinalPropertyGetter")
public static class EntityWithFinalPropertyGetter {
@Id
private int id;
@Version
@Column(name = "ver")
private int version;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public final String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity( name = "EntityWithFinalPropertySetter")
public static class EntityWithFinalPropertySetter {
@Id
private int id;
@Version
@Column(name = "ver")
private int version;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getName() {
return name;
}
public final void setName(String name) {
this.name = name;
}
}
}