Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproducer for HHH-15634 Lazy basic property does not get updated on change #5449

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -36,6 +39,7 @@
public class LazyBasicFieldAccessTest extends BaseCoreFunctionalTestCase {

private LazyEntity entity;

private Long entityId;

@Override
Expand All @@ -53,82 +57,98 @@ protected void configure(Configuration configuration) {
public void prepare() {
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = new LazyEntity();
entity.setDescription( "desc" );
entity.description = "desc";
s.persist( entity );
entityId = entity.id;
} );
}

@Test
public void test() {
public void testAttachedUpdate() {
doInHibernate( this::sessionFactory, s -> {
entity = s.get( LazyEntity.class, entityId );

Assert.assertFalse( isPropertyInitialized( entity, "description" ) );
checkDirtyTracking( entity );

assertEquals( "desc", entity.description );
assertTrue( isPropertyInitialized( entity, "description" ) );
} );

doInHibernate( this::sessionFactory, s -> {
entity = s.get( LazyEntity.class, entityId );
Assert.assertFalse( isPropertyInitialized( entity, "description" ) );
entity.description = "desc1";

checkDirtyTracking( entity, "description" );

assertEquals( "desc1", entity.description );
assertTrue( isPropertyInitialized( entity, "description" ) );
} );

doInHibernate( this::sessionFactory, s -> {
entity = s.get( LazyEntity.class, entityId );
assertEquals( "desc1", entity.description );
} );
}

@Test
@TestForIssue(jiraKey = "HHH-11882")
public void testDetachedUpdate() {
doInHibernate( this::sessionFactory, s -> {
entity = s.get( LazyEntity.class, entityId );

Assert.assertFalse( isPropertyInitialized( entity, "description" ) );
checkDirtyTracking( entity );

assertEquals( "desc", entity.getDescription() );
assertEquals( "desc", entity.description );
assertTrue( isPropertyInitialized( entity, "description" ) );
} );

doInHibernate( this::sessionFactory, s -> {
entity.setDescription( "desc1" );
entity.description = "desc1";
s.update( entity );

//Assert.assertFalse( Hibernate.isPropertyInitialized( entity, "description" ) );
checkDirtyTracking( entity, "description" );

assertEquals( "desc1", entity.getDescription() );
assertEquals( "desc1", entity.description );
assertTrue( isPropertyInitialized( entity, "description" ) );
} );

doInHibernate( this::sessionFactory, s -> {
entity = s.get( LazyEntity.class, entityId );
assertEquals( "desc1", entity.getDescription() );
assertEquals( "desc1", entity.description );
} );

doInHibernate( this::sessionFactory, s -> {
entity.setDescription( "desc2" );
entity.description = "desc2";
LazyEntity mergedEntity = (LazyEntity) s.merge( entity );

// Assert.assertFalse( isPropertyInitialized( entity, "description" ) );
//Assert.assertFalse( Hibernate.isPropertyInitialized( entity, "description" ) );
checkDirtyTracking( mergedEntity, "description" );

assertEquals( "desc2", mergedEntity.getDescription() );
assertEquals( "desc2", mergedEntity.description );
assertTrue( isPropertyInitialized( mergedEntity, "description" ) );
} );

doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "desc2", entity.getDescription() );
entity = s.get( LazyEntity.class, entityId );
assertEquals( "desc2", entity.description );
} );
}

// --- //

@Entity
@Table( name = "LAZY_FIELD_ENTITY" )
@Access( AccessType.FIELD )
@Table( name = "LAZY_PROPERTY_ENTITY" )
private static class LazyEntity {
Long id;
String description;

@Id
@GeneratedValue
Long getId() {
return id;
}

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

@Basic( fetch = FetchType.LAZY )
String getDescription() {
return description;
}

void setDescription(String description) {
this.description = description;
}
String description;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -38,7 +37,6 @@
public class LazyBasicPropertyAccessTest extends BaseCoreFunctionalTestCase {

private LazyEntity entity;

private Long entityId;

@Override
Expand All @@ -56,69 +54,111 @@ protected void configure(Configuration configuration) {
public void prepare() {
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = new LazyEntity();
entity.description = "desc";
entity.setDescription( "desc" );
s.persist( entity );
entityId = entity.id;
entityId = entity.getId();
} );
}

@Test
public void testAttachedUpdate() {
doInHibernate( this::sessionFactory, s -> {
entity = s.get( LazyEntity.class, entityId );

Assert.assertFalse( isPropertyInitialized( entity, "description" ) );
checkDirtyTracking( entity );

assertEquals( "desc", entity.getDescription() );
assertTrue( isPropertyInitialized( entity, "description" ) );
} );

doInHibernate( this::sessionFactory, s -> {
entity = s.get( LazyEntity.class, entityId );
Assert.assertFalse( isPropertyInitialized( entity, "description" ) );
entity.setDescription( "desc1" );

checkDirtyTracking( entity, "description" );

assertEquals( "desc1", entity.getDescription() );
assertTrue( isPropertyInitialized( entity, "description" ) );
} );

doInHibernate( this::sessionFactory, s -> {
entity = s.get( LazyEntity.class, entityId );
assertEquals( "desc1", entity.getDescription() );
} );
}

@Test
public void execute() {
@TestForIssue(jiraKey = "HHH-11882")
public void testDetachedUpdate() {
doInHibernate( this::sessionFactory, s -> {
entity = s.get( LazyEntity.class, entityId );

Assert.assertFalse( isPropertyInitialized( entity, "description" ) );
checkDirtyTracking( entity );

assertEquals( "desc", entity.description );
assertEquals( "desc", entity.getDescription() );
assertTrue( isPropertyInitialized( entity, "description" ) );
} );

doInHibernate( this::sessionFactory, s -> {
entity.description = "desc1";
entity.setDescription( "desc1" );
s.update( entity );

// Assert.assertFalse( Hibernate.isPropertyInitialized( entity, "description" ) );
checkDirtyTracking( entity, "description" );

assertEquals( "desc1", entity.description );
assertEquals( "desc1", entity.getDescription() );
assertTrue( isPropertyInitialized( entity, "description" ) );
} );

doInHibernate( this::sessionFactory, s -> {
entity = s.get( LazyEntity.class, entityId );
assertEquals( "desc1", entity.description );
assertEquals( "desc1", entity.getDescription() );
} );

doInHibernate( this::sessionFactory, s -> {
entity.description = "desc2";
entity.setDescription( "desc2" );
LazyEntity mergedEntity = (LazyEntity) s.merge( entity );

//Assert.assertFalse( Hibernate.isPropertyInitialized( entity, "description" ) );
// Assert.assertFalse( isPropertyInitialized( entity, "description" ) );
checkDirtyTracking( mergedEntity, "description" );

assertEquals( "desc2", mergedEntity.description );
assertEquals( "desc2", mergedEntity.getDescription() );
assertTrue( isPropertyInitialized( mergedEntity, "description" ) );
} );

doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "desc2", entity.description );
entity = s.get( LazyEntity.class, entityId );
assertEquals( "desc2", entity.getDescription() );
} );
}

// --- //

@Entity
@Access( AccessType.FIELD )
@Table( name = "LAZY_PROPERTY_ENTITY" )
@Table( name = "LAZY_FIELD_ENTITY" )
private static class LazyEntity {
Long id;
String description;

@Id
@GeneratedValue
Long id;
Long getId() {
return id;
}

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

@Basic( fetch = FetchType.LAZY )
String description;
String getDescription() {
return description;
}

void setDescription(String description) {
this.description = description;
}
}
}
Loading