HHH-16613 Add a test case to display an issue

This commit is contained in:
marko-bekhta 2023-05-16 12:45:26 +02:00 committed by Andrea Boriero
parent a64490ed50
commit f6ec141b47
2 changed files with 414 additions and 0 deletions

View File

@ -0,0 +1,206 @@
/*
* 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.sql.exec.manytoone;
import org.hibernate.Hibernate;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Cacheable;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import static org.assertj.core.api.Assertions.assertThat;
@DomainModel(
annotatedClasses = {
EntityWithEagerManyToOneTest.ConcreteEntity.class,
EntityWithEagerManyToOneTest.LazyAbstractEntityReference.class,
EntityWithEagerManyToOneTest.LazyConcreteEntityReference.class
}
)
@ServiceRegistry
@SessionFactory
public class EntityWithEagerManyToOneTest {
private Integer abstractEntityReferenceId;
private Integer concreteEntityReferenceId;
@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
ConcreteEntity entity = new ConcreteEntity();
session.save( entity );
LazyAbstractEntityReference reference = new LazyAbstractEntityReference( entity );
session.save( reference );
this.abstractEntityReferenceId = reference.getId();
LazyConcreteEntityReference concreteReference = new LazyConcreteEntityReference( entity );
session.save( concreteReference );
this.concreteEntityReferenceId = concreteReference.getId();
} );
}
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "update LazyAbstractEntityReference e set e.entity = null" ).executeUpdate();
session.createQuery( "update LazyConcreteEntityReference e set e.entity = null" ).executeUpdate();
session.createQuery( "delete from ConcreteEntity" ).executeUpdate();
session.createQuery( "delete from LazyAbstractEntityReference" ).executeUpdate();
session.createQuery( "delete from LazyConcreteEntityReference" ).executeUpdate();
}
);
}
@Test
public void containsEagerAbstractEntity(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
LazyAbstractEntityReference reference = session.get(
LazyAbstractEntityReference.class, abstractEntityReferenceId );
assertThat( reference ).isNotNull();
assertThat( Hibernate.isInitialized( reference.getEntity() ) ).isTrue();
}
);
}
@Test
public void containsEagerConcreteEntity(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
LazyConcreteEntityReference reference = session.get(
LazyConcreteEntityReference.class, concreteEntityReferenceId );
assertThat( reference ).isNotNull();
assertThat( Hibernate.isInitialized( reference.getEntity() ) ).isTrue();
}
);
}
@Entity(name = "AbstractEntity")
@Cacheable // NOTE that all these entities are cacheable - if removed from the entities both tests above - pass.
public static abstract class AbstractEntity {
@Id
@GeneratedValue
private Integer id;
public AbstractEntity() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
@Entity(name = "ConcreteEntity")
@Cacheable
public static class ConcreteEntity extends AbstractEntity {
private String content = "text";
public ConcreteEntity() {
super();
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
@Entity(name = "LazyAbstractEntityReference")
@Cacheable
public static class LazyAbstractEntityReference {
@Id
@GeneratedValue
private Integer id;
@ManyToOne(fetch = FetchType.EAGER)
private AbstractEntity entity;
public LazyAbstractEntityReference() {
}
public LazyAbstractEntityReference(AbstractEntity entity) {
setEntity( entity );
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public AbstractEntity getEntity() {
return entity;
}
public void setEntity(AbstractEntity entity) {
this.entity = entity;
}
}
@Entity(name = "LazyConcreteEntityReference")
@Cacheable
public static class LazyConcreteEntityReference {
@Id
@GeneratedValue
private Integer id;
@ManyToOne(fetch = FetchType.EAGER)
private ConcreteEntity entity;
public LazyConcreteEntityReference() {
}
public LazyConcreteEntityReference(ConcreteEntity entity) {
setEntity( entity );
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public ConcreteEntity getEntity() {
return entity;
}
public void setEntity(ConcreteEntity entity) {
this.entity = entity;
}
}
}

View File

@ -0,0 +1,208 @@
/*
* 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.sql.exec.manytoone;
import static org.assertj.core.api.Assertions.assertThat;
import org.hibernate.Hibernate;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Cacheable;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
@DomainModel(
annotatedClasses = {
EntityWithLazyManyToOneTest.ConcreteEntity.class,
EntityWithLazyManyToOneTest.LazyAbstractEntityReference.class,
EntityWithLazyManyToOneTest.LazyConcreteEntityReference.class
}
)
@ServiceRegistry
@SessionFactory
@JiraKey( "HHH-16613" )
public class EntityWithLazyManyToOneTest {
private Integer abstractEntityReferenceId;
private Integer concreteEntityReferenceId;
@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
ConcreteEntity entity = new ConcreteEntity();
session.save( entity );
LazyAbstractEntityReference reference = new LazyAbstractEntityReference( entity );
session.save( reference );
this.abstractEntityReferenceId = reference.getId();
LazyConcreteEntityReference concreteReference = new LazyConcreteEntityReference( entity );
session.save( concreteReference );
this.concreteEntityReferenceId = concreteReference.getId();
} );
}
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "update LazyAbstractEntityReference e set e.entity = null" ).executeUpdate();
session.createQuery( "update LazyConcreteEntityReference e set e.entity = null" ).executeUpdate();
session.createQuery( "delete from ConcreteEntity" ).executeUpdate();
session.createQuery( "delete from LazyAbstractEntityReference" ).executeUpdate();
session.createQuery( "delete from LazyConcreteEntityReference" ).executeUpdate();
}
);
}
@Test
public void containsLazyAbstractEntity(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
LazyAbstractEntityReference reference = session.get(
LazyAbstractEntityReference.class, abstractEntityReferenceId );
assertThat( reference ).isNotNull();
assertThat( Hibernate.isInitialized( reference.getEntity() ) ).isFalse();
}
);
}
@Test
public void containsLazyConcreteEntity(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
LazyConcreteEntityReference reference = session.get(
LazyConcreteEntityReference.class, concreteEntityReferenceId );
assertThat( reference ).isNotNull();
assertThat( Hibernate.isInitialized( reference.getEntity() ) ).isFalse();
}
);
}
@Entity(name = "AbstractEntity")
@Cacheable // NOTE that all these entities are cacheable - if removed from the entities both tests above - pass.
public static abstract class AbstractEntity {
@Id
@GeneratedValue
private Integer id;
public AbstractEntity() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
@Entity(name = "ConcreteEntity")
@Cacheable
public static class ConcreteEntity extends AbstractEntity {
private String content = "text";
public ConcreteEntity() {
super();
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
@Entity(name = "LazyAbstractEntityReference")
@Cacheable
public static class LazyAbstractEntityReference {
@Id
@GeneratedValue
private Integer id;
@ManyToOne(fetch = FetchType.LAZY)
private AbstractEntity entity;
public LazyAbstractEntityReference() {
}
public LazyAbstractEntityReference(AbstractEntity entity) {
setEntity( entity );
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public AbstractEntity getEntity() {
return entity;
}
public void setEntity(AbstractEntity entity) {
this.entity = entity;
}
}
@Entity(name = "LazyConcreteEntityReference")
@Cacheable
public static class LazyConcreteEntityReference {
@Id
@GeneratedValue
private Integer id;
@ManyToOne(fetch = FetchType.LAZY)
private ConcreteEntity entity;
public LazyConcreteEntityReference() {
}
public LazyConcreteEntityReference(ConcreteEntity entity) {
setEntity( entity );
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public ConcreteEntity getEntity() {
return entity;
}
public void setEntity(ConcreteEntity entity) {
this.entity = entity;
}
}
}