Spring Data JPA和Generics

时间:2016-05-31 19:03:16

标签: spring hibernate jpa spring-boot spring-data-jpa

我有一个看起来像这样的实体

@Entity(name = "encounter_pdf_export")
public class EncounterPDFExport<T extends Encounter> implements Serializable {

    public static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long pdfExportId;

    @Any(metaColumn = @Column(name = "encounter_type"))
    @Cascade(CascadeType.ALL)
    @AnyMetaDef(
            idType = "long",
            metaType = "string",
            metaValues = {
                    @MetaValue(value = "FooEncounter", targetEntity = FooEncounter.class)
            })
    @JoinColumn(name = "encounter_id")
    private T encounter;

我延伸的抽象类型是:

public abstract class Encounter {

    public abstract Long getEncounterId();
}

这是我的Spring Data Repository

@Repository
public interface EncounterPDFExportRepository extends PagingAndSortingRepository<EncounterPDFExport, Long> {

    EncounterPDFExport findOneByEncounter_encounterId(@Param("encounterId") Long encounterId);
}

启动与findOneByEncounter_encounterId方法相关的应用程序时,我得到了堆栈跟踪:

Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [encounter] on this ManagedType [com.iimassociates.distiller.domain.EncounterPDFExport]
    at org.hibernate.jpa.internal.metamodel.AbstractManagedType.checkNotNull(AbstractManagedType.java:144)
    at org.hibernate.jpa.internal.metamodel.AbstractManagedType.getAttribute(AbstractManagedType.java:130)
    at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:468)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.getTypedPath(JpaQueryCreator.java:300)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:243)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:148)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:88)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:46)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:116)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:237)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:65)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:100)

我假设Spring Data JPA不支持抽象/通用字段?如果是这样的话,创建@Query是否足以成为一种解决方法?

1 个答案:

答案 0 :(得分:3)

不确定这对任何人都有帮助,但我确实这样做了。

  1. 删除抽象类并使其成为具有单个公共getEncounterId()方法的接口
  2. 修改FooEncounter以实现上述接口
  3. 从EncounterPDFExport类中删除了泛型
  4. 修改了遭遇字段以使用上述界面而不是通用
  5. 显然,我在访问FooEncounter中的字段时遇到了一些Hibernate错误/限制。但是,在EncounterPDFExport中访问Encounter工作正常。我修改了我的Spring Data JPA Repository,看起来如下所示(请注意,遇到的遇到的修改来自于meet.encounterId和刚刚遭遇):

    @Repository
    public interface EncounterPDFExportRepository extends PagingAndSortingRepository<EncounterPDFExport, Long> {
    
        EncounterPDFExport findOneByEncounter(@Param("encounter") Encounter encounter);
    }
    
  6. 有问题的Hibernate错误似乎与https://jira.spring.io/browse/DATAJPA-836有关。