Skip to content

Commit

Permalink
HHH-15921 Add test for issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Jan 25, 2023
1 parent f9b1692 commit 8d0729f
Show file tree
Hide file tree
Showing 5 changed files with 1,091 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package org.hibernate.orm.test.batch;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel;
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.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@DomainModel(
annotatedClasses = {
BatchAndClassIdAndLazyCollectionTest.Child.class,
BatchAndClassIdAndLazyCollectionTest.Parent.class
}
)
@SessionFactory(
useCollectingStatementInspector = true
)
@TestForIssue(jiraKey = "HHH-15921")
public class BatchAndClassIdAndLazyCollectionTest {

@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
for (int i = 0; i< 10; i++) {
Parent parent = new Parent( (long) i );
Child child = new Child( (long) ( i + 1 ), parent );
Child child2 = new Child( (long) ( i + 2 ), parent );
Child child3 = new Child( (long) ( i + 3 ), parent );
Child child4 = new Child( (long) ( i + 4 ), parent );
Child child5 = new Child( (long) ( i + 5 ), parent );
Child child6 = new Child( (long) ( i + 6 ), parent );
Child child7 = new Child( (long) ( i + 7 ), parent );
Child child8 = new Child( (long) ( i + 8 ), parent );
Child child9 = new Child( (long) ( i + 9 ), parent );
Child child10 = new Child( (long) ( i + 10 ), parent );
Child child11 = new Child( (long) ( i + 11 ), parent );

session.persist( parent );
}
}
);
}

@Test
public void testFind(SessionFactoryScope scope){

scope.inTransaction(
session -> {
Parent parent = session.find( Parent.class, 1L );
assertThat(parent.getChildren().size()).isEqualTo( 11 );
}
);
}

@Test
public void testSelectChild(SessionFactoryScope scope){
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
scope.inTransaction(
session -> {
statementInspector.clear();
List<Child> children = session.createQuery( "select c from Child c", Child.class ).getResultList();
statementInspector.assertExecutedCount( 1 );
statementInspector.clear();
for ( Child c : children ) {
c.getParent().getName();
}
statementInspector.assertExecutedCount( 2 );
assertThat( statementInspector.getSqlQueries()
.get( 0 )
.toLowerCase( Locale.ROOT )
.contains( "in(?,?,?,?,?)" ) ).isTrue();
assertThat( statementInspector.getSqlQueries()
.get( 1 )
.toLowerCase( Locale.ROOT )
.contains( "in(?,?,?,?,?)" ) ).isTrue();
}
);
}

@Test
public void testGetReference(SessionFactoryScope scope){
scope.inTransaction(
session -> {
Parent parent = session.getReference( Parent.class, 1l );
Parent parent1 = session.getReference( Parent.class, 2l );
Parent parent2 = session.getReference( Parent.class, 3l );
assertFalse( Hibernate.isInitialized( parent ) );
assertFalse( Hibernate.isInitialized( parent1 ) );
assertFalse( Hibernate.isInitialized( parent2 ) );

parent.getName();

assertTrue( Hibernate.isInitialized( parent ) );
assertTrue( Hibernate.isInitialized( parent1 ) );
assertTrue( Hibernate.isInitialized( parent2 ) );

}
);
}

@Entity(name = "Child")
@Table(name = "child_tablle")
@IdClass(Child.IdClass.class)
public static class Child {
@Id
private Long id;

private String name;

@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Parent parent;

public Child() {
}

public Child(Long id, Parent parent) {
this.id = id;
this.name = String.valueOf( id );
this.parent = parent;
parent.addChild( this );
}

public static class IdClass implements Serializable {
private long id;

private Parent parent;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public Parent getParent() {
return parent;
}
}

@Entity(name = "Parent")
@Table(name = "parents")
@BatchSize(size = 5)
public static class Parent {
@Id
private Long id;

private String name;

@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Child> children = new HashSet<>();

public Parent() {
}

public Parent(Long id) {
this.id = id;
this.name = String.valueOf( id );
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public Set<Child> getChildren() {
return children;
}

public void addChild(Child child){
children.add( child );
}
}
}

0 comments on commit 8d0729f

Please sign in to comment.