HHH-13439 Encode Hibernate ORM version into build time enhanced entities

This commit is contained in:
Christian Beikov 2022-09-30 20:27:44 +02:00
parent ee08db2a1e
commit f9afd3dcb7
3 changed files with 58 additions and 0 deletions

View File

@ -22,11 +22,13 @@ import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.Transient;
import org.hibernate.Version;
import org.hibernate.bytecode.enhance.internal.tracker.CompositeOwnerTracker;
import org.hibernate.bytecode.enhance.internal.tracker.DirtyTracker;
import org.hibernate.bytecode.enhance.spi.CollectionTracker;
import org.hibernate.bytecode.enhance.spi.EnhancementContext;
import org.hibernate.bytecode.enhance.spi.EnhancementException;
import org.hibernate.bytecode.enhance.spi.EnhancementInfo;
import org.hibernate.bytecode.enhance.spi.Enhancer;
import org.hibernate.bytecode.enhance.spi.EnhancerConstants;
import org.hibernate.bytecode.enhance.spi.UnloadedField;
@ -68,6 +70,21 @@ import net.bytebuddy.pool.TypePool;
public class EnhancerImpl implements Enhancer {
private static final CoreMessageLogger log = CoreLogging.messageLogger( Enhancer.class );
private static final Annotation HIBERNATE_VERSION_ANNOTATION;
static {
HIBERNATE_VERSION_ANNOTATION = new EnhancementInfo() {
@Override
public String version() {
return Version.getVersionString();
}
@Override
public Class<? extends Annotation> annotationType() {
return EnhancementInfo.class;
}
};
}
protected final ByteBuddyEnhancementContext enhancementContext;
private final ByteBuddyState byteBuddyState;
@ -160,6 +177,8 @@ public class EnhancerImpl implements Enhancer {
return null;
}
builder = builder.annotateType( HIBERNATE_VERSION_ANNOTATION );
if ( enhancementContext.isEntityClass( managedCtClass ) ) {
log.debugf( "Enhancing [%s] as Entity", managedCtClass.getName() );
builder = builder.implement( ManagedEntity.class )

View File

@ -0,0 +1,25 @@
/*
* 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.bytecode.enhance.spi;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Provides basic information about the enhancement done to a class.
*/
@Target( ElementType.TYPE )
@Retention( RetentionPolicy.RUNTIME )
public @interface EnhancementInfo {
/**
* The Hibernate version used for enhancement.
*/
String version();
}

View File

@ -6,9 +6,13 @@
*/
package org.hibernate.orm.test.bytecode.enhancement.basic;
import org.hibernate.Version;
import org.hibernate.bytecode.enhance.spi.EnhancementInfo;
import org.hibernate.engine.spi.ManagedEntity;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.orm.test.legacy.Simple;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils;
import org.junit.Assert;
@ -23,6 +27,7 @@ import java.util.List;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
@ -56,6 +61,15 @@ public class BasicEnhancementTest {
assertSame( managedEntity, managedEntity.$$_hibernate_getPreviousManagedEntity() );
}
@Test
@TestForIssue(jiraKey = "HHH-13439")
public void enhancementInfoTest() {
EnhancementInfo info = SimpleEntity.class.getAnnotation( EnhancementInfo.class );
assertNotNull( "EnhancementInfo was not applied", info );
assertEquals( Version.getVersionString(), info.version() );
}
@Test
public void basicInterceptableTest() {
SimpleEntity entity = new SimpleEntity();