我有一个基础抽象类。
@Entity
@Table(name = "P_FLD")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "FLD_DISCRIMINATOR", columnDefinition = "CHAR(3)")
abstract public class AbstractPassbookField
和一些扩展它的类。例如:
@Entity
@DiscriminatorValue("F")
@Table(name = "P_FLD_F")
public class PassbookFileField extends AbstractPassbookField
我为基础实体创建存储库
public interface PassbookRepository extends CrudRepository<AbstractPassbookField, Long>
我正在进行下一次测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:test-config.xml")
public class PassbookFieldRepositoryTest {
@Autowired
PassbookRepository passbookRepository;
@PersistenceContext
private EntityManager em;
@Test
public void testSave() {
PassbookFileField passbookFileField = new PassbookFileField();
passbookFileField.setFilename("text.test");
passbookFileField.setTemplate(true);
passbookFileField.setReadonly(true);
passbookFileField.setImageType(ImageType.I);
passbookFileField.setResoltuionType(ImageResolutionType.N);
passbookFileField = passbookRepository.save(passbookFileField);
passbookRepository.findAll();
}
}
passbookRepository.save(passbookFileField) - 效果很好,但是 passbookRepository.findAll()给了我一个例外
org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: Object [id=1] was not of the specified subclass [ru.teamlabs.moneybox.commons.model.passbook.field.AbstractPassbookField] : Discriminator: F ; nested exception is org.hibernate.WrongClassException: Object [id=1] was not of the specified subclass [ru.teamlabs.moneybox.commons.model.passbook.field.AbstractPassbookField] : Discriminator: F
通过entityManager查询给出了同样的错误。我做错了什么?
答案 0 :(得分:0)
您还没有为超级类提供DiscriminatorValue
值,因此在检索它时无法区分超类和子类。尝试以下,它必须工作。
@Entity
@Table(name = "P_FLD")
@DiscriminatorValue("SF")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "FLD_DISCRIMINATOR", columnDefinition = "CHAR(3)")
abstract public class AbstractPassbookField
答案 1 :(得分:0)
我发现了它为什么会发生。
@DiscriminatorColumn(name = "FLD_DISCRIMINATOR", columnDefinition = "CHAR(3)")
这个字符串就是问题所在。
在PassbookFileField中我有
@DiscriminatorValue("F")
但是,存储库期望获得带有3个字符的鉴别器的实体。
这种鉴别者
@DiscriminatorValue("F")
或此类鉴别器列定义
@DiscriminatorColumn(name = "FLD_DISCRIMINATOR", columnDefinition = "CHARACTER VARYING(3)")
解决问题