我无法使用hql

时间:2017-04-01 12:49:35

标签: spring-boot repository spring-data-jpa hql spring-restcontroller

我有两个类:Person和Computer我想获取属于某个人的计算机列表

@Entity
public class Person implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id_pperson;
    private String name;
    private String last_name;
    @OneToMany(mappedBy = "Person", fetch = FetchType.LAZY)
    private Collection<Computer> pc;
    public void setPc(Collection<Computer> pc) {
        this.pc = pc;
    }
    public Collection<Computer> getPc() {
        return pc;
    }
}
@Entity
public class Computer implements Serializable {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
        private String brand;
    @ManyToOne
    @JoinColumn(name="id_owner", referencedColumnName = "id_person", insertable = true)
    private Person Person;
    public long getid_owner()
    {
        return Person.getId_person();
    }
}

所以我在ComputerRepository中添加了一个方法,用外键id_owner

获取计算机
public interface ComputerRepository extends JpaRepository<FinancementExt, Long>{

@Query("select c from Computer c where c.id_owner = :id")
public Page <FinancementExt> findAllByIdOwner(@Param("id")long id_owner,Pageable pageable);}

这是服务

public interface ComputerService { public Page<Computer> listFEByComputer(long id_owner,int page, int size);       }

    @Service public class ComputerServiceImp implements ComputerService {
       @Autowired
       ComputerRepository CRepository;
       @Override
        public Page<Computer> listFEByComputer(long id_owner, int page, int size) {
         return CRepository.findAllByIdOwner(id_owner, new PageRequest(page, size));
      }

}

这是其余的控制器:

@RestController  public class ComputerRestService {
@Autowired
private ComputerService computerService ;
@RequestMapping(value="/listcomputersByperson",method=RequestMethod.GET)
public Page<Computer> listFEByComputer(long id_owner,int page, int size) {
    return ComputerService.listFEByComputer(id_owner,page, size);
}

}

这些是我得到的赘述:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ComputerServiceImp ': Unsatisfied dependency expressed through field 'CompuerRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CompuerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract org.springframework.data.domain.Page com.projet.dao.CompuerRepository.findAllByIdOwner(long,org.springframework.data.domain.Pageable)!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at com.projet.SuiviProjetApplication.main(SuiviProjetApplication.java:17) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_111]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_111]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_111]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.1.RELEASE.jar:1.5.1.RELEASE]

引起:org.springframework.beans.factory.BeanCreationException:创建名为'CompuerRepository'的bean时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:查询方法public abstract org.springframework.data.domain.Page com.projet.dao.CompuerRepository.findAllByIdOwner(long,org.springframework.data.domain.Pageable)的验证失败!     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)〜[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)~ [spring-beans-4.3.6.RELEASE.jar:4.3.6 。发布]     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)~ [spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)〜[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]     ...省略了24个常用帧     引起:org.hibernate.QueryException:无法解析属性:id_prj:com.projet.entities.FinancementExt     在org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:62)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:56)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1764)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:393)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:505)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:660)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:264)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:204)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:109)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:104)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:942)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1283)~ [hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4696)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4166)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2131)~ [hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:810)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:605)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:309)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:257)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     ...省略了62个常见帧

1 个答案:

答案 0 :(得分:0)

@Query("select c from Computer c where c.id_owner = :id")
public Page <FinancementExt> findAllByIdOwner(@Param("id")long id_owner,Pageable pageable);

首先,查询选择计算机,但该方法返回Page<FinancementExt>。这毫无意义。

其次,Computer类中没有名为id_owner的持久字段。查询应该是

select c from Computer c where c.person.id = :id

并且字段Person应命名为person。尊重基本的Java命名约定。