HHH-10865 - ManyToMany relation dropped from database when lazy loading is active

This commit is contained in:
barreiro 2016-06-24 06:06:44 +01:00 committed by Andrea Boriero
parent 6cc9f6242a
commit f0dfc1269b
6 changed files with 172 additions and 5 deletions

View File

@ -59,4 +59,6 @@ public interface BytecodeEnhancementMetadata {
LazyAttributeLoadingInterceptor extractInterceptor(Object entity) throws NotInstrumentedException;
boolean hasUnFetchedAttributes(Object entity);
boolean isAttributeLoaded(Object entity, String attributeName);
}

View File

@ -15,6 +15,7 @@ import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
@ -495,13 +496,13 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer {
@Override
public Object[] getPropertyValues(Object entity) {
boolean getAll = shouldGetAllProperties( entity );
final BytecodeEnhancementMetadata enhancementMetadata = entityMetamodel.getBytecodeEnhancementMetadata();
final int span = entityMetamodel.getPropertySpan();
final Object[] result = new Object[span];
for ( int j = 0; j < span; j++ ) {
NonIdentifierAttribute property = entityMetamodel.getProperties()[j];
if ( getAll || !property.isLazy() ) {
if ( !property.isLazy() || enhancementMetadata.isAttributeLoaded( entity, property.getName() ) ) {
result[j] = getters[j].get( entity );
}
else {

View File

@ -57,4 +57,10 @@ public class BytecodeEnhancementMetadataNonPojoImpl implements BytecodeEnhanceme
public boolean hasUnFetchedAttributes(Object entity) {
return false;
}
@Override
public boolean isAttributeLoaded(Object entity, String attributeName) {
return true;
}
}

View File

@ -67,12 +67,16 @@ public class BytecodeEnhancementMetadataPojoImpl implements BytecodeEnhancementM
@Override
public boolean hasUnFetchedAttributes(Object entity) {
LazyAttributeLoadingInterceptor interceptor = isEnhancedForLazyLoading()
? extractInterceptor( entity )
: null;
LazyAttributeLoadingInterceptor interceptor = enhancedForLazyLoading ? extractInterceptor( entity ) : null;
return interceptor != null && interceptor.hasAnyUninitializedAttributes();
}
@Override
public boolean isAttributeLoaded(Object entity, String attributeName) {
LazyAttributeLoadingInterceptor interceptor = enhancedForLazyLoading ? extractInterceptor( entity ) : null;
return interceptor == null || interceptor.isAttributeLoaded( attributeName );
}
@Override
public LazyAttributeLoadingInterceptor extractInterceptor(Object entity) throws NotInstrumentedException {
if ( !enhancedForLazyLoading ) {

View File

@ -25,6 +25,7 @@ import org.hibernate.test.bytecode.enhancement.join.HHH3949TestTask2;
import org.hibernate.test.bytecode.enhancement.join.HHH3949TestTask3;
import org.hibernate.test.bytecode.enhancement.join.HHH3949TestTask4;
import org.hibernate.test.bytecode.enhancement.lazy.HHH_10708.UnexpectedDeleteOneTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.HHH_10708.UnexpectedDeleteThreeTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.HHH_10708.UnexpectedDeleteTwoTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.LazyBasicFieldNotInitializedTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.LazyCollectionLoadingTestTask;
@ -116,6 +117,7 @@ public class EnhancerTest extends BaseUnitTestCase {
public void testLazyUnexpectedDelete() {
EnhancerTestUtils.runEnhancerTestTask( UnexpectedDeleteOneTestTask.class );
EnhancerTestUtils.runEnhancerTestTask( UnexpectedDeleteTwoTestTask.class );
EnhancerTestUtils.runEnhancerTestTask( UnexpectedDeleteThreeTestTask.class );
}
@Test

View File

@ -0,0 +1,152 @@
/*
* 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.HHH_10708;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
import org.junit.Assert;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class UnexpectedDeleteThreeTestTask extends AbstractEnhancerTestTask {
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Parent.class, Child.class };
}
@Override
public void prepare() {
Configuration cfg = new Configuration();
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
prepare( cfg );
Session s = getFactory().openSession();
s.beginTransaction();
Child child = new Child();
child.setId( 2L );
s.save(child);
Parent parent = new Parent();
parent.setId( 1L );
parent.setNames( Collections.singleton( "name" ) );
Set<Child> children = new HashSet<Child>();
children.add(child);
parent.setChildren( children );
s.save( parent );
s.getTransaction().commit();
s.clear();
s.close();
}
@Override
public void execute() {
Session s = getFactory().openSession();
s.beginTransaction();
Parent parent = s.get( Parent.class, 1L );
Set<Child> children = parent.getChildren();
if (children == null) {
children = new HashSet<Child>();
parent.setChildren( children );
}
Child child = new Child();
child.setId( 1L );
s.save(child);
children.add(child);
// We need to leave at least one attribute unfetchd
//parent.getNames().size();
s.save(parent);
s.getTransaction().commit();
s.close();
s = getFactory().openSession();
s.beginTransaction();
Parent application = s.get( Parent.class, 1L );
Assert.assertEquals( "Loaded Children collection has unexpected size", 2, application.getChildren().size() );
s.getTransaction().commit();
s.close();
}
@Override
protected void cleanup() {
}
// --- //
@Entity public static class Child {
private Long id;
@Id
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
return id;
}
public void setId(Long id)
{
this.id = id;
}
}
@Entity public static class Parent {
private Long id;
private Set<String> names;
private Set<Child> children;
@Id @Column(name = "id", unique = true, nullable = false)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ElementCollection
public Set<String> getNames() {
return names;
}
public void setNames(Set<String> secrets) {
this.names = secrets;
}
@ManyToMany(fetch = FetchType.LAZY, targetEntity = Child.class)
public Set<Child> getChildren() {
return children;
}
public void setChildren(Set<Child> children) {
this.children = children;
}
}
}