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

(cherry picked from commit 8e8be9aeb4)

Conflicts:
	hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java
	hibernate-core/src/main/java/org/hibernate/tuple/entity/BytecodeEnhancementMetadataPojoImpl.java
This commit is contained in:
Gail Badner 2016-06-29 17:19:25 -07:00
parent 11f2d5d5f7
commit 4c900da689
5 changed files with 181 additions and 2 deletions

View File

@ -14,7 +14,9 @@ import java.util.Set;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.bytecode.instrumentation.spi.FieldInterceptor;
import org.hibernate.bytecode.instrumentation.spi.LazyPropertyInitializer;
import org.hibernate.bytecode.spi.EntityInstrumentationMetadata;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
@ -491,13 +493,16 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer {
@Override
public Object[] getPropertyValues(Object entity) throws HibernateException {
boolean getAll = shouldGetAllProperties( entity );
final int span = entityMetamodel.getPropertySpan();
final Object[] result = new Object[span];
final EntityInstrumentationMetadata enhancementMetadata = entityMetamodel.getInstrumentationMetadata();
final FieldInterceptor interceptor = enhancementMetadata.isInstrumented()
? enhancementMetadata.extractInterceptor( entity )
: null;
for ( int j = 0; j < span; j++ ) {
NonIdentifierAttribute property = entityMetamodel.getProperties()[j];
if ( getAll || !property.isLazy() ) {
if ( !property.isLazy() || interceptor == null || interceptor.isInitialized( property.getName() ) ) {
result[j] = getters[j].get( entity );
}
else {

View File

@ -24,6 +24,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;
@ -106,6 +107,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,10 @@
/*
* 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;
public class Child {
}

View File

@ -0,0 +1,10 @@
/*
* 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;
public class Parent {
}

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;
}
}
}