Hibernate:继承和关系映射+泛型



我使用的是带有hibernate的spring数据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的情况下,hibernate会创建不一致和错误的!!!外键,即从test_containable到registration_compound(但不是从Batch到test_compound,这里它只正确地映射到registration _compound)。

在可控制的方面,我使用什么策略似乎并不重要。

现在来谈谈我测试中的实际问题。具体的测试类别。有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会出现问题

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=?

并且在另一个选择语句中选择CCD_ 1。因此,它们总共有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));
}

如果这个测试作为第二个或第三个运行,我会得到错误的类异常!!!有人知道这里到底发生了什么吗?解决方案

问题是映射之一:

@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;
}

此映射缺少目标实体。正确的是

@ManyToOne(optional=true, fetch = FetchType.EAGER, cascade = CascadeType.ALL, targetEntity = Compound.class)

出于某种原因,hibernate只是假设目标是RegistrationCompound,而不是抛出异常。很烦人,否则很容易发现问题。但就这样,它几乎把我逼疯了。

最新更新