我正在将 spring-data JPA 与休眠一起使用。我很难让我的继承和关系映射正常工作。
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name="compound")
@DiscriminatorColumn(name="compound_type")
@DiscriminatorOptions(force=true)
public abstract class Compound<T extends Containable> {
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.compound",
cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
private List<CompoundComposition> compositions = new ArrayList<>();
@OneToMany(fetch = FetchType.EAGER, mappedBy="compound",
targetEntity=Containable.class, cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private Set<T> containables = new HashSet<T>();
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name="containable")
@DiscriminatorColumn(name="containable_type")
@DiscriminatorOptions(force=true)
public abstract class Containable<T extends Compound> {
@ManyToOne(optional=true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private T compound;
}
这个想法是 AbstractCompound 的某个实现只能与 Containable 的一个特定实现相关联(反之亦然)。这导致以下实现:
@Entity
@DiscriminatorValue("TestCompound")
public class TestCompound extends AbstractCompound<TestContainable> {
}
@Entity
@DiscriminatorValue("RegistrationCompound")
public class RegistrationCompound extends AbstractCompound<Batch> {
@Column(name = "reg_number", unique = true)
private String regNumber;
}
@Entity
@DiscriminatorValue("TestContainable")
public class TestContainable extends Containable<TestCompound> {
}
@Entity
@DiscriminatorValue("Batch")
public class Batch extends Containable<RegistrationCompound>{
@Column(name = "batch_number")
private Integer batchNumber;
}
我玩过所有的继承策略,对于复合层次结构,单表是唯一至少部分有效的策略。在 JOINED 或表 _per_class 休眠的情况下创建不一致和错误!!!外键,即从 test_containable 到 registration_compound(但不是从 Batch 到 test_compound,这里它仅正确映射到 registration_compound)。
在 Containable 方面,我使用什么策略似乎并不重要。
现在到我的测试中的实际问题。具体的测试类。有3个测试。所有人都在对“TestCompound”实例进行特定搜索。问题是这 3 个测试用例中的第一个执行总是通过,其他 2 个总是失败。运行的顺序似乎是随机的(JUnit + @RunWith(SpringJUnit4ClassRunner.class))。这意味着任何测试都通过,如果它是第一个运行的。
失败的测试抛出以下异常:
org.hibernate.WrongClassException: Object with id: 1000 was not of the specified
subclass: RegistrationCompound (loaded object was of wrong class class TestCompound)
在第一次测试的情况下,在正确选择用于获取 Containables 后出现休眠问题
Hibernate: select containabl0_.compound_id as compound8_1_1_, containabl0_.id as id0_1_,
containabl0_.id as id0_0_, containabl0_.created as created0_0_,
containabl0_.created_by as created4_0_0_, containabl0_.last_modified as last5_0_0_,
containabl0_.last_modified_by as last6_0_0_, containabl0_.compound_id as compound8_0_0_,
containabl0_.batch_number as batch7_0_0_, containabl0_.containable_type as containa1_0_0_
from containable containabl0_ where containabl0_.containable_type in ('Batch', 'TestContainable')
and containabl0_.compound_id=?
并且List<CompoundComposition> compositions
在另一个选择语句中被选中。所以它们总共是 3 条语句:获取化合物、获取可包含物、获取组合物。
对于第二个和第三个测试,用于获取可包含项的 SQL 与用于获取组合的 on 合并,并且它的构建方式是尝试选择 RegistrationCompound 而不是 TestCompound,例如它包含
registrati1_.reg_number as reg10_1_0_,
并且 reg_number 只是 RegistrationCompound 的属性。在这两种情况下,第一个正确选择实际化合物的 select 语句在 where 子句中包含以下内容:
testcompou0_.compound_type='TestCompound'
所以这非常令人困惑。为什么它取决于运行测试的顺序?为什么它会尝试选择 RegistrationCompound?
这是 3 个测试中最简单的测试:
@Test
@Transactional
public void testFindByCompositionPkStructureId() {
System.out.println("findByCompositionPkStructureId");
Long structureId = 1000L;
TestCompound compound = new TestCompound();
compound.setId(1000L);
compound.setCas("9999-99-9");
compound.setCompoundName("Test Compound");
List<TestCompound> result =
testCompoundRepository.findByCompositionsPkStructureId(structureId);
assertEquals(compound, result.get(0));
}
如果此测试作为第二个或第三个运行,我会得到错误的类异常!!!有谁知道这里到底发生了什么?解决方案?