IntelliJ IDEA无法解析spring data jpa @query注解中的实体

23

这是我的repository接口:

public interface ContentRepository extends JpaRepository<Content, Long> {

    @Query(value = "select c from Content c where c.ContentCategory.genre =  :genre and c.ContentType.genre = :contentType")
    Iterable<Content> findByTypeAndCategory(@Param("contentType") String contentType, @Param("genre") String genre);

}

以下是Content POJO:

@Entity
@Table(name = "content")
public class Content implements Serializable {

public Content() {
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@ManyToOne
private ContentCategory contentCategory;

@ManyToOne
private ContentType contentType;

// other methods }

这是我的applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd 
       http://www.springframework.org/schema/data/jpa
       http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

<tx:annotation-driven/>
<context:component-scan base-package="com.aa.bb"/>
<jpa:repositories base-package="com.aa.bb.repository"/>


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test1"/>
    <property name="username" value="root"/>
    <property name="password" value="2323"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="com.tarameshgroup.derakht.repository"/>

    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL"/>
            <property name="showSql" value="true"/>
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

编译器无法在@Query中找到Content


它在Eclipse中能工作吗? - SkorpEN
@SkorpEN 我没有Eclipse,我正在使用IntelliJ IDEA。 - CVV
最简单的JPA示例在您的IntelliJ中是否有效? - SkorpEN
复制编译器错误信息。 - SkorpEN
1
你在面向配置中选择了默认的JPA提供程序吗? - John Harris
显示剩余9条评论
7个回答

2

三个问题:

  1. 您没有激活JPA facet(这是在@Query中下划线内容类型的原因)
  2. 您的包设置不正确,packageToScan应该设置为您的实体包
  3. 您应该在@Query中引用属性,而不是类型(我看到奇怪的大写content.ContentCategory.genre)

之后,您应该看到@Query中解析的Content类型,并且能够正确运行查询


1
public interface ContentRepository extends JpaRepository<Content, Long> {

@Query(value = "select c from Content c where c.ContentCategory.genre =  :genre and c.ContentType.genre = :contentType")
Iterable<Content> findByTypeAndCategory(@Param("contentType") String contentType, @Param("genre") String genre);

我很确定这是一个大写问题:

@Query(value = "select c from Content c where c.contentCategory.genre =  :genre and c.contentType.genre = :contentType")

你使用了类型(例如,c.ContentCategory),而不是字段名称(例如,c.contentCategory)。

0

您没有引用实体的包。请确保它在您在applicationContext.xml中设置的类路径扫描中。


0
一些原因可能会导致这种情况发生。首先尝试的事情是确保您正确设置了 JPA Facet:
  • 进入项目结构,选择模块并检查列表中的模块是否有 JPA facet。如果没有,则添加一个。

  • 如果仅此不能解决问题,则尝试查看您的 Spring 配置。您已经展示了要扫描实体的包是 com.tarameshgroup.derakht.repository

    <bean id="entityManagerFactory" class="..."> ... <property name="packagesToScan" value="com.tarameshgroup.derakht.repository"/>

    但是,这是否是实体所在的确切包(您的代码没有显示)。我曾经遇到过 IDEA 的问题,即使我定义了顶级包,应用程序也可以正确运行,但是 IDEA 并不会自动捕捉到它们以进行语法高亮。例如,如果我有一个包 com.foo.bar.domain,其中包含一些实体,以及 com.foo.bar.domain.subpkg,我的 Spring 配置中有一个 packagesToScan 值只是 com.foo.bar.domain,那么该包中的实体将具有正确的高亮显示,但子包则没有。答案是更改 Spring 配置的 packagesToScan 值以包括子包,即使 Spring 本身不需要这样做,例如 com.foo.bar.domain,com.foo.bar.domain.subpkg


0
c.contentCategory.genre

而不是

c.ContentCategory.genre

c.contentType.genre

而不是

c.ContentType.genre

0

IntelliJ IDEA在spring data jpa @query注释中不总是解析实体,因为您可以在应用程序启动时创建/修改实体,例如使用Hibernate create和/或create-drop。因此,它会在运行时解析实体。

为了避免出现此错误,您需要正确设置IntelliJ的设置-请参阅IDEA IntelliJ的文档。

您可能需要在IntelliJ中添加“配置JPA或Hibernate facet”快速修复。

另一个可能的解决方案是在实体POJO中使用NamedQuery。示例代码:

@NamedQueries(
 {          
 // AclEntries
 @NamedQuery(
      name = GET_CONTENT_BY_GENRE_AND_CONTENT_TYPE,
      query = "select c from Content c where c.ContentCategory.genre =  :genre and c.ContentType.genre = :contentType"
 )
 }
)
@Entity
@Table(name = "content")
public class Content implements Serializable {
public static final String GET_CONTENT_BY_GENRE_AND_CONTENT_TYPE = "Content.selecCfromContentWhereGenreAndContentType";
public Content() {
}
...
}

你可以这样使用它:

List contentResults = entityManager.createNamedQuery(Content.GET_CONTENT_BY_GENRE_AND_CONTENT_TYPE)
.setParameter("genre", "parameter-genre-value")
.setParameter("contentType","parameter-contentType-value")
.getResultList();

另一个可能的解决方案是在实体POJO中使用NamedQuery。示例代码: - Binyamin Regev

-1

有两种解决方案:

  1. 使用 persistence.xml 创建 JPA facet
  2. 在数据库选项卡中连接到 jdbc:mysql://localhost:3306/test1(通常在右侧边栏中)

第二种解决方案已经解决了我在Intellij查询高亮方面遇到的大部分问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接