23

Here is my repository interface:

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);

}

And Here is 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 }

And here my 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>

compiler can't find Content in @Query

CVV
  • 451
  • 2
  • 7
  • 21

7 Answers7

2

Three problems:

  1. You don't have the JPA facet activated (this is what underlines the Content type in @Query in red)
  2. Your packages are incorrectly set, packageToScan should be set to your entities package
  3. You should reference properties in @Query, not types (I see weird content.ContentCategory.genre in uppercase)

After that, you should see Content type resolved in @Query AND be able to run your query correctly

rdlopes
  • 661
  • 4
  • 11
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);

I am pretty sure that there is a capitalization issue:

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

You had the types (e.g., c.ContentCategory), not the field names (e.g., c.contentCategory).

Kristian H
  • 174
  • 5
0

You haven't quoted the package of your Entity. Please make sure it is in the classpath scan you have set in the applicationContext.xml

rainerhahnekamp
  • 1,087
  • 12
  • 27
0

A few things can cause this. First thing to try is to make sure you have a JPA Facet correctly setup:

  • Go to Project Structure, select Modules and check your module in the list has a JPA facet. If it doesn't then add one.

  • If that alone doesn't help, then also try looking at your Spring config. You've show that the package(s) to scan for entities is com.tarameshgroup.derakht.repository

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

    However is that the exact package that the entity resides in (your code doesn't show). I've had IDEA problems where I have sub-packages and although the app runs correctly if I define the top-level package as you have, IDEA doesn't automatically pick them up for syntax highlighting purposes. So, for example, if I have package com.foo.bar.domain with some entities in, as well as com.foo.bar.domain.subpkg and my Spring config had a packagesToScan value of just com.foo.bar.domain then the entities in that package would have correct highlighting, but not the sub-package. The answer was to change the Spring configured packagesToScan value to include the sub-packages, even though Spring itself doesn't require this, i.e. com.foo.bar.domain, com.foo.bar.domain.subpkg

D.C.
  • 1
0
c.contentCategory.genre

instead

c.ContentCategory.genre

and

c.contentType.genre

instead

c.ContentType.genre
Jakub Pomykała
  • 2,082
  • 3
  • 27
  • 58
0

IntelliJ IDEA doesn't always resolve entity in spring data jpa @query annotation, it resolve it in runtime since you can create/modify entities when your application starts, e.g. using Hibernate create and/or create-drop. There's a IntelliJ setting that you need to set correctly in order not to get this error - see IDEA IntelliJ's documentation.

You may need to add "Configure JPA or Hibernate facet" quick fix in IntelliJ.

Another possible solution can be using NamedQuery in the entity POJO. Example code:

@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() {
}
...
}

You use it like this:

List contentResults = entityManager.createNamedQuery(Content.GET_CONTENT_BY_GENRE_AND_CONTENT_TYPE)
.setParameter("genre", "parameter-genre-value")
.setParameter("contentType","parameter-contentType-value")
.getResultList();
Binyamin Regev
  • 914
  • 5
  • 19
  • 31
-1

Two solutions come to mind:

  1. Create a JPA facet with the persistence.xml
  2. Connect to jdbc:mysql://localhost:3306/test1 in your database tab (usually in the right sidebar)

The second solution has fixed most of the problems I had with Intellij query highlighting.

Thogor
  • 321
  • 2
  • 7