HHH-11655 : test case

This commit is contained in:
Gail Badner 2017-09-08 00:00:14 -07:00
parent 7c4bc03c3a
commit 97e7dc59e6
3 changed files with 175 additions and 0 deletions

View File

@ -0,0 +1,60 @@
/*
* 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.annotations.tuplizer.bytebuddysubclass;
import java.io.Serializable;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.tuple.Instantiator;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;
/**
* @author Florian Bien
*/
public class MyEntityInstantiator implements Instantiator {
private final PersistentClass persistentClass;
public MyEntityInstantiator(PersistentClass persistentClass) {
this.persistentClass = persistentClass;
}
@Override
public Object instantiate(Serializable id) {
return instantiate();
}
@Override
public Object instantiate() {
return createInstance( persistentClass.getMappedClass() );
}
public static <E> E createInstance(Class<E> entityClass) {
Class<? extends E> loaded = new ByteBuddy()
.subclass( entityClass )
.method( ElementMatchers.named( "toString" ) )
.intercept( FixedValue.value( "transformed" ) )
.make()
.load( entityClass.getClassLoader(), ClassLoadingStrategy.Default.INJECTION )
.getLoaded();
try {
return loaded.newInstance();
}
catch (Exception e) {
throw new RuntimeException( "Unable to create new instance of " + entityClass.getSimpleName(), e );
}
}
@Override
public boolean isInstance(Object object) {
return true;
}
}

View File

@ -0,0 +1,55 @@
/*
* 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.annotations.tuplizer.bytebuddysubclass;
import org.hibernate.EntityNameResolver;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.tuple.Instantiator;
import org.hibernate.tuple.entity.EntityMetamodel;
import org.hibernate.tuple.entity.PojoEntityTuplizer;
/**
* @author Florian Bien
*/
public class MyTuplizer extends PojoEntityTuplizer {
public MyTuplizer(
EntityMetamodel entityMetamodel,
PersistentClass mappedEntity) {
super( entityMetamodel, mappedEntity );
}
public EntityNameResolver[] getEntityNameResolvers() {
return new EntityNameResolver[] { MyEntityNameResolver.INSTANCE };
}
@Override
protected Instantiator buildInstantiator(EntityMetamodel entityMetamodel, PersistentClass persistentClass) {
return new MyEntityInstantiator( persistentClass );
}
public static class MyEntityNameResolver implements EntityNameResolver {
public static final MyEntityNameResolver INSTANCE = new MyEntityNameResolver();
public String resolveEntityName(Object entity) {
if ( entity.getClass().getName().contains( "$ByteBuddy$" ) ) {
return entity.getClass().getSuperclass().getName();
}
else {
return entity.getClass().getName();
}
}
public boolean equals(Object obj) {
return getClass().equals( obj.getClass() );
}
public int hashCode() {
return getClass().hashCode();
}
}
}

View File

@ -0,0 +1,60 @@
/*
* 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.annotations.tuplizer.bytebuddysubclass;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.Session;
import org.hibernate.annotations.Tuplizer;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Florian Bien
*/
public class TuplizerInstantiatesByteBuddySubclassTest extends BaseCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { SimpleEntity.class };
}
@Test
public void hhh11655Test() throws Exception {
Session session = openSession();
session.beginTransaction();
SimpleEntity simpleEntityNonProxy = new SimpleEntity();
Assert.assertFalse( session.contains( simpleEntityNonProxy ) );
SimpleEntity simpleEntity = MyEntityInstantiator.createInstance( SimpleEntity.class );
Assert.assertFalse( session.contains( simpleEntity ) );
session.persist( simpleEntity );
Assert.assertTrue( session.contains( simpleEntity ) );
session.getTransaction().rollback();
session.close();
}
@Entity(name = "SimpleEntity")
@Tuplizer(impl = MyTuplizer.class)
public static class SimpleEntity {
protected SimpleEntity() {
}
@Id
@GeneratedValue
private Long id;
}
}