HHH-17140 Add test for issue

This commit is contained in:
Andrea Boriero 2023-09-01 12:25:10 +02:00 committed by Andrea Boriero
parent 4e524f67c5
commit 1efc90754e
2 changed files with 259 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package org.hibernate.orm.test.annotations.inheritance;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
@ -24,6 +26,8 @@ import jakarta.persistence.Query;
import jakarta.persistence.Table;
import jakarta.persistence.Version;
import static org.assertj.core.api.Assertions.assertThat;
@Jpa(
annotatedClasses = {
@ -69,7 +73,12 @@ public class SingleTableInheritanceLazyAssociationTest {
for ( Message message : resultList ) {
Address address = message.getAddress();
assertThat( Hibernate.isInitialized( address ) ).isFalse();
address.getId();
assertThat( Hibernate.isInitialized( address ) ).isTrue();
User user = address.getUser();
assertThat( Hibernate.isInitialized( user ) ).isTrue();
assertThat( Hibernate.isInitialized( user.getAddress() ) ).isTrue();
}
}
);
@ -205,6 +214,8 @@ public class SingleTableInheritanceLazyAssociationTest {
public String getId() {
return this.userId;
}
public abstract Address getAddress();
}
@Entity(name = "UserA")
@ -222,6 +233,11 @@ public class SingleTableInheritanceLazyAssociationTest {
super( userId );
}
@Override
public AddressA getAddress() {
return addressA;
}
public void setAddressA(AddressA addressA) {
this.addressA = addressA;
}
@ -242,6 +258,11 @@ public class SingleTableInheritanceLazyAssociationTest {
super( userId );
}
@Override
public AddressB getAddress() {
return addressB;
}
public void setAddressB(AddressB addressB) {
this.addressB = addressB;
}

View File

@ -0,0 +1,238 @@
package org.hibernate.orm.test.associations;
import java.util.Arrays;
import java.util.List;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import static jakarta.persistence.ConstraintMode.NO_CONSTRAINT;
@DomainModel(
annotatedClasses = {
ManyToOneBidirectionalTest.OrderEntity.class,
ManyToOneBidirectionalTest.OrderItem.class,
ManyToOneBidirectionalTest.Product.class,
ManyToOneBidirectionalTest.Sku.class,
}
)
@SessionFactory
@JiraKey("HHH-17140")
public class ManyToOneBidirectionalTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Product p = new Product( 1l, "abc" );
Sku sku = new Sku( 1l, "sku", p );
OrderItem item = new OrderItem( 1L, sku, p );
OrderEntity orderEntity = new OrderEntity( 1l );
orderEntity.setItems( Arrays.asList( item ) );
session.persist( p );
session.persist( sku );
session.persist( item );
session.persist( orderEntity );
}
);
}
@Test
public void testFindFolloweByAGet(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
OrderEntity order = session.find( OrderEntity.class, 1l );
order.getItems().get( 0 );
Sku responseItem = session.get( Sku.class, 1l );
}
);
}
@Entity(name = "OrderEntity")
public static class OrderEntity {
@Id
Long id;
@OneToMany
List<OrderItem> items;
public OrderEntity() {
}
public OrderEntity(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<OrderItem> getItems() {
return items;
}
public void setItems(List<OrderItem> items) {
this.items = items;
}
}
@Entity(name = "OrderItem")
public static class OrderItem {
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "SKU_ID", nullable = false)
private Sku sku;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PRODUCT_ID")
protected Product product;
public OrderItem() {
}
public OrderItem(Long id, Sku sku, Product product) {
this.id = id;
this.sku = sku;
this.product = product;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Sku getSku() {
return sku;
}
public void setSku(Sku sku) {
this.sku = sku;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
@Entity(name = "Product")
public static class Product {
@Id
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "DEFAULT_SKU_ID")
private Sku defaultSku;
public Product() {
}
public Product(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Sku getDefaultSku() {
return defaultSku;
}
public void setDefaultSku(Sku defaultSku) {
this.defaultSku = defaultSku;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity(name = "Sku")
public static class Sku {
@Id
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "DEFAULT_PRODUCT_ID", foreignKey = @ForeignKey(NO_CONSTRAINT))
protected Product defaultProduct;
public Sku() {
}
public Sku(Long id, String name, Product product) {
this.id = id;
this.name = name;
this.defaultProduct = product;
defaultProduct.setDefaultSku( this );
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Product getDefaultProduct() {
return defaultProduct;
}
public void setDefaultProduct(Product defaultProduct) {
this.defaultProduct = defaultProduct;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}