我可以使用Spring数据jpa生成的存储库与@Query和继承策略=加入

时间:2015-02-19 22:18:21

标签: java spring hibernate jpa

在spring jpa和hibernate应用程序中(如果重要,则为mysql), 我有一个父类,而不是像这样定义的抽象:



    @Entity
    @Table(name = "PUSH_EVENTS")
    @Inheritance(strategy = InheritanceType.JOINED)
    public class PushEventEntity {

        @Id
        @Column(name = "event_id")
        @GeneratedValue(generator = "uuid")
        @GenericGenerator(name = "uuid", strategy = "uuid2")
        private String eventId;//this is the foreign key for the child entities

        @Column(name = "item_id")
        private String    itemId;
        @Column(name = "event_price")
        private Double    eventPrice;

    //other properties omitted

和一个儿童班:



    @Entity(name = "BID_RECEIVED_EVENTS")
    @PrimaryKeyJoinColumn(name = "event_id")
    public class BidReceivedEventEntity extends PushEventEntity {

        @Column(name = "listing_bid_count")
        private Integer listingBidCount;
    //other properties omitted

我有一个子类的存储库:



    public interface BidReceivedEventsRepository extends JpaRepository {

        @Query("select b FROM BidReceivedEventEntity b JOIN PushEventEntity p " +
            "WHERE (p.eventId=b.eventId and p.itemId =:itemId and p.eventPrice=:eventPrice)")
        public Iterable searchBidsByItemIdAndPrice(@Param("itemId") String itemId, @Param("eventPrice") double eventPrice);

如您所见,我正在尝试按itemid和price(在父级中定义)获取所有BidReceivedEventEntity对象。

试着运行这个,我得到:



    Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.lang.Iterable com.ebay.inv.mobile.persistence.push.repo.BidReceivedEventsRepository.searchBidsByItemIdAndPrice(java.lang.String,double)!
        at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:92)
        at org.springframework.data.jpa.repository.query.SimpleJpaQuery.(SimpleJpaQuery.java:62)
        at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72)
        at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:53)
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:135)
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:203)
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:72)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:349)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:187)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225)
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
        ... 82 more
    Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: BidReceivedEventEntity is not mapped [select b FROM BidReceivedEventEntity b JOIN PushEventEntity p WHERE (p.eventId=b.eventId and p.itemId =:itemId and p.eventPrice=:eventPrice)]
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:331)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)
        at com.sun.proxy.$Proxy75.createQuery(Unknown Source)
        at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86)
        ... 95 more
    Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: BidReceivedEventEntity is not mapped [select b FROM BidReceivedEventEntity b JOIN PushEventEntity p WHERE (p.eventId=b.eventId and p.itemId =:itemId and p.eventPrice=:eventPrice)]
        at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
        at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
        at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:131)
        at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:93)
        at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
        at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
        at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
        at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
        ... 102 more
    Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: BidReceivedEventEntity is not mapped
        at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
        at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
        at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)
        at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:332)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
        ... 110 more

我可以像这样使用JPA吗? 我没有父类的存储库,因为我不需要直接访问父对象。

我哪里出错了? 谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

来自stacktrace:

QuerySyntaxException: BidReceivedEventEntity is not mapped

尝试声明完全限定名称,例如:com.mycompany.model.BidReceivedEventEntity