具有对象继承的存储库中的Spring Data自定义查询

时间:2016-02-27 16:18:09

标签: spring spring-security spring-data

我正在使用Spring MVC + Data + Security,我遇到了Spring Data问题。我有3个实体(它们之间具有不同的属性),它们扩展到另一个实体,我试图根据父实体从其中一个实体进行查询。

这里是代码:

Perfil (表示配置文件,但当我尝试使用配置文件时遇到问题)是其他 3个类的父类或超类

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Perfil {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    ...

这是,它来自 Perfil

@Entity
@PrimaryKeyJoinColumn(name = "perfil_id")
public class Person extends Perfil {

    private String firstName;

    private String lastName;

    private Gender gender;

我有 PersonRepository 。它有一种方法,我尝试使用 Perfil ID

找到
@Repository
public interface PersonRepository extends CrudRepository<Person, Integer> {
    @Query("SELECT p FROM Person p WHERE p.perfil_id=?1")
    Person findByProfileID(Integer id);
}

我收到此错误

Caused by: org.hibernate.QueryException: could not resolve property: perfil_id of: com.giulianok.person.Person [SELECT p FROM com.giulianok.person.Person p WHERE p.perfil_id=?1]
    at org.hibernate.QueryException.generateQueryException(QueryException.java:137) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
    ... 96 common frames omitted

我理解的是,Spring无法找到&#34; perfil_id&#34;在Person内部,这是有道理的,因为我没有那个属性,但最后,当我看一下数据库时,我看到了列&#34; perfil_id&#34;

问题是,如何根据对象继承进行查询?

1 个答案:

答案 0 :(得分:1)

您正在使用JPQL,因此您需要在查询中使用object属性,而不是数据库列:

RewriteCond %{REQUEST_URI} !^/robots\.txt$
RewriteCond %{REQUEST_URI} !^/index\.html$

RewriteCond %{REQUEST_URI} !json$
RewriteRule ^(.*)$ http://www.siteurl.com [R=permanent,L]

请注意,如果属性名称与数据库列名称不同,则应使用@Column批注将对象属性映射到右列。即,如果Perfil对象的属性id映射到perfil_id列,您应该:

@Repository
public interface PersonRepository extends CrudRepository<Person, Integer> {
    @Query("SELECT p FROM Person p WHERE p.id=?1")
    Person findByProfileID(Integer id);
}