继承类型已加入JPA - 在Spring JPA中查询属性

时间:2015-06-15 13:59:48

标签: spring jpa spring-data-jpa

我无法启动服务器,因为它抱怨在查询中找不到继承的属性。

出于讨论目的,我已经设置了这样的类层次结构:

BaseContent
==============
id
createUser

Category
===============
id
otherProperties

我已经宣布了类似的类型:

@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "base_content")
public class BaseContent  {
...
}

@Entity
@Table(name="categories")
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name = "base_content_id", referencedColumnName = "id")
public class Category extends BaseContent{
}

请注意,相关属性都有getter / setter等。

我正在使用Spring Data / JPA,我的服务器和Intellij都抱怨无法为此查询找到继承的属性createUser(等等):

@Query("select c from Category c " +
            "left join fetch c.createUser " +
            "left join fetch c.lastUpdateUser " +
            "left join fetch c.galleries g " +
            "left join fetch g.media " +
            "left join fetch c.parentRelationship pr " +
            "left join fetch c.productCategoryRelationships pcr " +
            "left join fetch pcr.child " +
            "left join fetch c.appliedLayouts l " +
            "left join fetch pr.parent " +
            "where c.id = ?1")
    public Category findById(Long categoryId);

我的问题是,因为我正在尝试将其移动到继承模型(已加入),如何在带注释的查询中引用继承的属性?

(注意该属性是BaseContent中公共getter / setter的私有可见性)

谢谢

1 个答案:

答案 0 :(得分:0)

ah woops, I forgot the @Entity annotation on the superclass...

works now.