test for JPA-46 with a twist
I use a @DiscriminatorFormula and @Basic(optional=false) to do it in a nice way
This commit is contained in:
parent
a8620b62bb
commit
a4191c9e11
|
@ -0,0 +1,17 @@
|
||||||
|
package org.hibernate.orm.test.annotations.sharedfk;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import org.hibernate.annotations.DiscriminatorFormula;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = " INHERITANCE_TAB")
|
||||||
|
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||||
|
@Access(AccessType.FIELD)
|
||||||
|
//@DiscriminatorColumn(name = "DISC")
|
||||||
|
@DiscriminatorFormula("case when value1 is not null then 1 when value2 is not null then 2 end")
|
||||||
|
public class AbstractChild {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
@Column(name = "ID")
|
||||||
|
Integer id;
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package org.hibernate.orm.test.annotations.sharedfk;
|
||||||
|
|
||||||
|
import jakarta.persistence.Access;
|
||||||
|
import jakarta.persistence.AccessType;
|
||||||
|
import jakarta.persistence.Basic;
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.DiscriminatorValue;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Access(AccessType.FIELD)
|
||||||
|
@DiscriminatorValue("1")
|
||||||
|
public class ConcreteChild1 extends AbstractChild {
|
||||||
|
@Basic(optional = false)
|
||||||
|
@Column(name = "VALUE1")
|
||||||
|
String value;
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package org.hibernate.orm.test.annotations.sharedfk;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Access(AccessType.FIELD)
|
||||||
|
@DiscriminatorValue("2")
|
||||||
|
public class ConcreteChild2 extends AbstractChild {
|
||||||
|
@Basic(optional = false)
|
||||||
|
@Column(name = "VALUE2")
|
||||||
|
String value;
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package org.hibernate.orm.test.annotations.sharedfk;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "PARENT")
|
||||||
|
public class Parent {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
@Column(name = "ID")
|
||||||
|
Integer id;
|
||||||
|
|
||||||
|
@Column(name = "NAME")
|
||||||
|
String name;
|
||||||
|
|
||||||
|
@OneToMany( fetch= FetchType.EAGER)
|
||||||
|
@JoinColumn(name = "PARENT_ID")
|
||||||
|
@OrderColumn(name = "ORDER_C")
|
||||||
|
|
||||||
|
List<ConcreteChild1> child1s = new LinkedList<>();
|
||||||
|
@OneToMany( fetch= FetchType.EAGER)
|
||||||
|
@JoinColumn(name = "PARENT_ID")
|
||||||
|
@OrderColumn(name = "ORDER_C")
|
||||||
|
List<ConcreteChild2> child2s= new LinkedList<>();
|
||||||
|
}
|
|
@ -0,0 +1,81 @@
|
||||||
|
package org.hibernate.orm.test.annotations.sharedfk;
|
||||||
|
|
||||||
|
import org.hibernate.PropertyValueException;
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
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.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hibernate.Hibernate.isInitialized;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
@TestForIssue(jiraKey = "JPA-46")
|
||||||
|
@SessionFactory
|
||||||
|
@DomainModel(annotatedClasses = {AbstractChild.class, Parent.class, ConcreteChild1.class, ConcreteChild2.class})
|
||||||
|
public class TestDiscriminatedOneToMany {
|
||||||
|
@Test
|
||||||
|
void test(SessionFactoryScope scope) {
|
||||||
|
ConcreteChild1 child0 = new ConcreteChild1();
|
||||||
|
child0.value = "0";
|
||||||
|
ConcreteChild1 child1 = new ConcreteChild1();
|
||||||
|
child1.value = "1";
|
||||||
|
ConcreteChild2 child2 = new ConcreteChild2();
|
||||||
|
child2.value = "2";
|
||||||
|
Parent parent = new Parent();
|
||||||
|
parent.child1s = List.of( child0, child1 );
|
||||||
|
parent.child2s = List.of( child2 );
|
||||||
|
scope.inTransaction( s -> {
|
||||||
|
s.persist( child0 );
|
||||||
|
s.persist( child1 );
|
||||||
|
s.persist( child2 );
|
||||||
|
s.persist( parent );
|
||||||
|
} );
|
||||||
|
scope.inTransaction( s -> {
|
||||||
|
Parent p = s.find(Parent.class, parent.id);
|
||||||
|
assertEquals( 1, p.child2s.size() );
|
||||||
|
assertEquals( 2, p.child1s.size() );
|
||||||
|
} );
|
||||||
|
scope.inTransaction( s -> {
|
||||||
|
Parent p = s.createQuery("from Parent", Parent.class).getSingleResult();
|
||||||
|
assertTrue( isInitialized( p.child1s ) ); //mapped EAGER
|
||||||
|
assertTrue( isInitialized( p.child2s ) ); //mapped EAGER
|
||||||
|
assertEquals( 1, p.child2s.size() );
|
||||||
|
assertEquals( 2, p.child1s.size() );
|
||||||
|
} );
|
||||||
|
scope.inTransaction( s -> {
|
||||||
|
Parent p = s.createQuery("from Parent left join fetch child1s", Parent.class).getSingleResult();
|
||||||
|
assertTrue( isInitialized( p.child1s ) );
|
||||||
|
assertTrue( isInitialized( p.child2s ) ); //mapped EAGER
|
||||||
|
assertEquals( 1, p.child2s.size() );
|
||||||
|
assertEquals( 2, p.child1s.size() );
|
||||||
|
} );
|
||||||
|
scope.inTransaction( s -> {
|
||||||
|
Parent p = s.createQuery("from Parent join fetch child1s join fetch child2s", Parent.class).getSingleResult();
|
||||||
|
assertTrue( isInitialized( p.child1s ) );
|
||||||
|
assertTrue( isInitialized( p.child2s ) );
|
||||||
|
assertEquals( 1, p.child2s.size() );
|
||||||
|
assertEquals( 2, p.child1s.size() );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testNonoptionalEnforced(SessionFactoryScope scope) {
|
||||||
|
ConcreteChild1 child0 = new ConcreteChild1();
|
||||||
|
Parent parent = new Parent();
|
||||||
|
parent.child1s = List.of( child0 );
|
||||||
|
try {
|
||||||
|
scope.inTransaction( s -> {
|
||||||
|
s.persist( parent );
|
||||||
|
s.persist( child0 );
|
||||||
|
} );
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
catch (PropertyValueException e) {
|
||||||
|
assertTrue( e.getMessage().contains("ConcreteChild1.value") );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue