本文介绍了spring数据中的多态查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基础抽象类.

@Entity
@Table(name = "P_FLD")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "FLD_DISCRIMINATOR", columnDefinition = "CHAR(3)")
abstract public class AbstractPassbookField

和一些扩展它的类.例如:

and some classes that extends it. For example:

@Entity
@DiscriminatorValue("F")
@Table(name = "P_FLD_F")
public class PassbookFileField extends AbstractPassbookField

我为基础实体创建存储库

and i create repository for base entity

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() 给了我一个例外

passbookRepository.save(passbookFileField) - works well, butpassbookRepository.findAll() gives me an exception

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 查询给了我同样的错误.我做错了什么?

Quering through entityManager gives me the same error. What I'm doing wrong?

推荐答案

我已经找到了原因.

@DiscriminatorColumn(name = "FLD_DISCRIMINATOR", columnDefinition = "CHAR(3)")

这个字符串是问题所在.

This string was the problem.

在 PassbookFileField 我有

In PassbookFileField i have

@DiscriminatorValue("F")

但是存储库期望获得带有 3 个字符的鉴别器的实体.

But repository expected to get entity with discriminator with 3 chars.

这种判别器

@DiscriminatorValue("F")

或这样的鉴别器列定义

@DiscriminatorColumn(name = "FLD_DISCRIMINATOR", columnDefinition = "CHARACTER VARYING(3)")

解决问题

这篇关于spring数据中的多态查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 05:09