From 97e7dc59e68eb70366c32819fe321c46e02d1901 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 8 Sep 2017 00:00:14 -0700 Subject: [PATCH] HHH-11655 : test case --- .../MyEntityInstantiator.java | 60 +++++++++++++++++++ .../bytebuddysubclass/MyTuplizer.java | 55 +++++++++++++++++ ...izerInstantiatesByteBuddySubclassTest.java | 60 +++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/tuplizer/bytebuddysubclass/MyEntityInstantiator.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/tuplizer/bytebuddysubclass/MyTuplizer.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/tuplizer/bytebuddysubclass/TuplizerInstantiatesByteBuddySubclassTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/tuplizer/bytebuddysubclass/MyEntityInstantiator.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/tuplizer/bytebuddysubclass/MyEntityInstantiator.java new file mode 100644 index 0000000000..3cab644e71 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/tuplizer/bytebuddysubclass/MyEntityInstantiator.java @@ -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 . + */ +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 createInstance(Class entityClass) { + Class 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; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/tuplizer/bytebuddysubclass/MyTuplizer.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/tuplizer/bytebuddysubclass/MyTuplizer.java new file mode 100644 index 0000000000..971e32a170 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/tuplizer/bytebuddysubclass/MyTuplizer.java @@ -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 . + */ +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(); + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/tuplizer/bytebuddysubclass/TuplizerInstantiatesByteBuddySubclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/tuplizer/bytebuddysubclass/TuplizerInstantiatesByteBuddySubclassTest.java new file mode 100644 index 0000000000..bd72290e8a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/tuplizer/bytebuddysubclass/TuplizerInstantiatesByteBuddySubclassTest.java @@ -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 . + */ +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; + + } +} \ No newline at end of file