Merge branch 'main_2' into wip/6.0_merge_5

This commit is contained in:
Andrea Boriero 2021-05-24 16:00:11 +02:00
commit 466c9b5f38
4 changed files with 204 additions and 7 deletions

View File

@ -8,8 +8,8 @@ package org.hibernate;
import java.util.Iterator;
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.HibernateIterator;
import org.hibernate.engine.jdbc.LobCreator;
@ -202,11 +202,8 @@ public final class Hibernate {
if ( entity instanceof PersistentAttributeInterceptable ) {
PersistentAttributeInterceptor interceptor = ( (PersistentAttributeInterceptable) entity ).$$_hibernate_getInterceptor();
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
return false;
}
if ( interceptor instanceof LazyAttributeLoadingInterceptor ) {
return ( (LazyAttributeLoadingInterceptor) interceptor ).isAttributeLoaded( propertyName );
if ( interceptor instanceof BytecodeLazyAttributeInterceptor ) {
return ( (BytecodeLazyAttributeInterceptor) interceptor ).isAttributeLoaded( propertyName );
}
}

View File

@ -7,6 +7,8 @@
package org.hibernate.bytecode.internal.bytebuddy;
import java.util.Collections;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@ -29,6 +31,7 @@ public class BasicProxyFactoryImpl implements BasicProxyFactory {
private final Class proxyClass;
private final ProxyConfiguration.Interceptor interceptor;
private final Constructor proxyClassConstructor;
@SuppressWarnings({ "unchecked", "rawtypes" })
public BasicProxyFactoryImpl(Class superClass, Class[] interfaces, ByteBuddyState byteBuddyState) {
@ -50,12 +53,18 @@ public class BasicProxyFactoryImpl implements BasicProxyFactory {
.intercept( byteBuddyState.getProxyDefinitionHelpers().getInterceptorFieldAccessor() )
);
this.interceptor = new PassThroughInterceptor( proxyClass.getName() );
try {
proxyClassConstructor = proxyClass.getConstructor();
}
catch (NoSuchMethodException e) {
throw new AssertionFailure( "Could not access default constructor from newly generated basic proxy" );
}
}
@Override
public Object getProxy() {
try {
final ProxyConfiguration proxy = (ProxyConfiguration) proxyClass.newInstance();
final ProxyConfiguration proxy = (ProxyConfiguration) proxyClassConstructor.newInstance();
proxy.$$_hibernate_set_interceptor( this.interceptor );
return proxy;
}

View File

@ -0,0 +1,80 @@
/*
* 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.bytecode.enhancement.lazy;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.Hibernate;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@TestForIssue(jiraKey = "HHH-14571")
@RunWith(BytecodeEnhancerRunner.class)
@EnhancementOptions(lazyLoading = true, extendedEnhancement = true)
public class IdInUninitializedProxyTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { AnEntity.class };
}
@Override
protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) {
super.configureStandardServiceRegistryBuilder( ssrb );
ssrb.applySetting( AvailableSettings.ALLOW_ENHANCEMENT_AS_PROXY, true );
}
@Test
public void testIdIsAlwaysConsideredInitialized() {
inTransaction( session -> {
final AnEntity e = session.byId( AnEntity.class ).getReference( 1 );
assertFalse( Hibernate.isInitialized( e ) );
// This is the gist of the problem
assertTrue( Hibernate.isPropertyInitialized( e, "id" ) );
assertFalse( Hibernate.isPropertyInitialized( e, "name" ) );
assertEquals( "George", e.name );
assertTrue( Hibernate.isInitialized( e ) );
assertTrue( Hibernate.isPropertyInitialized( e, "id" ) );
assertTrue( Hibernate.isPropertyInitialized( e, "name" ) );
} );
}
@Before
public void prepareTestData() {
inTransaction( session -> {
AnEntity anEntity = new AnEntity();
anEntity.id = 1;
anEntity.name = "George";
session.persist( anEntity );
} );
}
@Entity(name = "AnEntity")
public static class AnEntity {
@Id
private int id;
@Basic
private String name;
}
}

View File

@ -0,0 +1,111 @@
package org.hibernate.test.hql;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.MapKeyJoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import static org.hamcrest.core.Is.is;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertThat;
/**
* @author Burkhard Graves
*/
@TestForIssue(jiraKey = "HHH-14475")
public class IndicesTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {Project.class, Role.class, Person.class};
}
@Before
public void setUp() {
doInHibernate( this::sessionFactory, session -> {
Project project = new Project(1);
Role role = new Role(1);
session.save( project );
session.save( role );
Person person = new Person(1, project, role);
session.save( person );
});
}
@Test
public void testSelectIndices() {
doInHibernate( this::sessionFactory, session -> {
List<Object> result = session.createQuery(
"select indices(p.roles) from Person p"
).list();
assertThat( result.size(), is( 1 ) );
});
}
@Entity(name = "Person")
public static class Person {
@Id
private Integer id;
@OneToMany
@JoinTable(name = "person_to_role",
joinColumns = @JoinColumn(name = "person_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
@MapKeyJoinColumn(name = "project_id")
private Map<Project, Role> roles;
public Person() {
}
public Person(Integer id, Project project, Role role) {
this.id = id;
roles = new HashMap<>();
roles.put(project, role);
}
}
@Entity(name = "Project")
public static class Project {
@Id
private Integer id;
public Project() {
}
public Project(Integer id) {
this.id = id;
}
}
@Entity(name = "Role")
public static class Role {
@Id
private Integer id;
public Role() {
}
public Role(Integer id) {
this.id = id;
}
}
}