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

2022-09-01 23:53:36

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

}

这是POJO:Content

@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>

编译器在 中找不到Content@Query


答案 1

三个问题:

  1. 您没有激活 JPA 分面(这是以红色@Query显示内容类型下划线的内容)
  2. 您的软件包设置不正确,软件包ToScan应设置为您的实体软件包
  3. 您应该以@Query而不是类型引用属性(我看到大写字母很奇怪)content.ContentCategory.genre

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


答案 2
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)。


推荐