IntelliJ IDEA无法解析spring数据jpa @query注释中的实体

CVV*_*CVV 22 java spring jpa intellij-idea

这是我的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);

}
Run Code Online (Sandbox Code Playgroud)

这是ContentPOJO:

@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 }
Run Code Online (Sandbox Code Playgroud)

在这里我的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>
Run Code Online (Sandbox Code Playgroud)

编译器不能找到Content@Query

Tho*_*gor -1

我想到了两种解决方案:

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

第二个解决方案解决了我在 Intellij 查询突出显示方面遇到的大部分问题。