Polymorphic query in spring data

你。 提交于 2021-02-08 05:17:52

问题


I have one base abstract class.

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

I'm running next test

@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) - works well, but passbookRepository.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 

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


回答1:


You haven't given the DiscriminatorValue value for your Super Class thus when retrieving it can not distinguish Super and Sub Classes. Try the following, it must work.

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



回答2:


I've found out why it was happening.

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

This string was the problem.

In PassbookFileField i have

@DiscriminatorValue("F")

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

Such discriminator

@DiscriminatorValue("F")

or such discriminator column definition

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

solves the problem



来源:https://stackoverflow.com/questions/24692429/polymorphic-query-in-spring-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!