-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
...va/org/hibernate/orm/test/inheritance/SingleTableInheritanceAndShareCacheModeAllTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package org.hibernate.orm.test.inheritance; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; | ||
import org.hibernate.testing.orm.junit.Jpa; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Inheritance; | ||
import jakarta.persistence.InheritanceType; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToMany; | ||
import jakarta.persistence.SharedCacheMode; | ||
import jakarta.persistence.criteria.CriteriaQuery; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
@Jpa( | ||
annotatedClasses = { | ||
SingleTableInheritanceAndShareCacheModeAllTest.Cheese.class, | ||
SingleTableInheritanceAndShareCacheModeAllTest.SpecialCheese.class, | ||
SingleTableInheritanceAndShareCacheModeAllTest.Hole.class | ||
}, | ||
sharedCacheMode = SharedCacheMode.ALL | ||
) | ||
@TestForIssue(jiraKey = "HHH-15699") | ||
public class SingleTableInheritanceAndShareCacheModeAllTest { | ||
|
||
@BeforeEach | ||
public void setUp(EntityManagerFactoryScope scope) { | ||
scope.inTransaction( | ||
entityManager -> { | ||
Cheese cheese = new Cheese(); | ||
entityManager.persist( cheese ); | ||
|
||
Hole hole = new Hole(); | ||
hole.setCheese( cheese ); | ||
entityManager.persist( hole ); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testCriteria(EntityManagerFactoryScope scope) { | ||
scope.inTransaction( | ||
entityManager -> { | ||
CriteriaQuery<Cheese> criteria = entityManager.getCriteriaBuilder().createQuery( Cheese.class ); | ||
List<Cheese> cheeses = entityManager.createQuery( criteria.select( criteria.from( Cheese.class ) ) ) | ||
.getResultList(); | ||
assertThat( cheeses.size() ).isEqualTo( 1 ); | ||
assertThat( cheeses.get( 0 ).getHoles().size() ).isEqualTo( 1 ); | ||
} | ||
); | ||
} | ||
|
||
@Entity(name = "Cheese") | ||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) | ||
public static class Cheese { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence") | ||
private Long id; | ||
|
||
private String name; | ||
|
||
@OneToMany(mappedBy = "cheese", fetch = FetchType.LAZY) | ||
private List<Hole> holes = new ArrayList<>(); | ||
|
||
public List<Hole> getHoles() { | ||
return holes; | ||
} | ||
|
||
public void addHole(Hole hole) { | ||
this.holes.add( hole ); | ||
} | ||
} | ||
|
||
@Entity(name = "SpecialCheese") | ||
public class SpecialCheese extends Cheese { | ||
} | ||
|
||
@Entity(name = "Hole") | ||
public static class Hole { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence") | ||
private Long id; | ||
|
||
@Column(name = "SIZE_COLUMN") | ||
private int size; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY, optional = false) | ||
@JoinColumn(name = "cheese", nullable = false) | ||
private Cheese cheese; | ||
|
||
public Cheese getCheese() { | ||
return cheese; | ||
} | ||
|
||
public void setCheese(Cheese cheese) { | ||
this.cheese = cheese; | ||
cheese.addHole( this ); | ||
} | ||
} | ||
|
||
} |