Skip to content

Commit

Permalink
HHH-15072 Don't enhance records and skip owner tracking for embeddabl…
Browse files Browse the repository at this point in the history
…e record fields
  • Loading branch information
beikov committed Aug 30, 2022
1 parent 9f5f31e commit d2cca54
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
Expand Up @@ -149,6 +149,11 @@ private DynamicType.Builder<?> doEnhance(DynamicType.Builder<?> builder, TypeDes
log.debugf( "Skipping enhancement of [%s]: it's an interface", managedCtClass.getName() );
return null;
}
// can't effectively enhance records
if ( managedCtClass.isRecord() ) {
log.debugf( "Skipping enhancement of [%s]: it's a record", managedCtClass.getName() );
return null;
}
// skip already enhanced classes
if ( alreadyEnhanced( managedCtClass ) ) {
log.debugf( "Skipping enhancement of [%s]: already enhanced", managedCtClass.getName() );
Expand Down
Expand Up @@ -62,7 +62,9 @@ else if ( !persistentField.hasAnnotation( Id.class )
}

if ( enhancementContext.isCompositeClass( persistentField.getType().asErasure() )
&& persistentField.hasAnnotation( Embedded.class ) ) {
&& persistentField.hasAnnotation( Embedded.class )
// Don't do composite owner tracking for records
&& !persistentField.getType().isRecord() ) {

// HHH-13759 - Call getter on superclass if field is not visible
// An embedded field won't be visible if declared private in a superclass
Expand Down
@@ -0,0 +1,101 @@
/*
* 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.orm.test.records;


import org.hibernate.engine.spi.ManagedComposite;
import org.hibernate.engine.spi.ManagedEntity;

import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.orm.junit.JiraKey;
import org.junit.Test;
import org.junit.runner.RunWith;

import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@JiraKey( "HHH-15072" )
@RunWith( BytecodeEnhancerRunner.class )
@EnhancementOptions(lazyLoading = true, extendedEnhancement = true, inlineDirtyChecking = true)
public class RecordAsEmbeddableEnhancementTest extends BaseCoreFunctionalTestCase {

@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { MyEntity.class };
}

@Test
public void test() {
// Ensure entity is enhanced, but not the record class
assertTrue( ManagedEntity.class.isAssignableFrom( MyEntity.class ) );
assertFalse( ManagedComposite.class.isAssignableFrom( MyRecord.class ) );

doInHibernate(
this::sessionFactory,
session -> {
session.persist( new MyEntity( 1L, new MyRecord( "test", "abc" ) ) );
}
);

doInHibernate(
this::sessionFactory,
session -> {
MyEntity myEntity = session.get( MyEntity.class, 1L );
assertNotNull( myEntity );
assertEquals( "test", myEntity.getRecord().name() );
assertEquals( "abc", myEntity.getRecord().description() );

myEntity.setRecord( new MyRecord( "test2", "def" ) );
}
);
}

@Entity(name = "MyEntity")
public static class MyEntity {
@Id
Long id;
@Embedded
MyRecord record;

public MyEntity() {
}

public MyEntity(Long id, MyRecord record) {
this.id = id;
this.record = record;
}

public Long getId() {
return id;
}

public MyRecord getRecord() {
return record;
}

public void setId(Long id) {
this.id = id;
}

public void setRecord(MyRecord record) {
this.record = record;
}
}

@Embeddable
public static record MyRecord(String name, String description) {}
}

0 comments on commit d2cca54

Please sign in to comment.