[英]IntelliJ IDEA can't resolve entity in spring data jpa @query annotation
Here is my repository
interface: 这是我的
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);
}
And Here is Content
POJO: 这是
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
: 在这里我的
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
编译器无法在
@Query
找到Content
Three problems: 三个问题:
content.ContentCategory.genre
in uppercase) content.ContentCategory.genre
大写) After that, you should see Content type resolved in @Query AND be able to run your query correctly 之后,您应该看到在@Query中解析的内容类型并且能够正确运行您的查询
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
请确保它位于applicationContext.xml中设置的类路径扫描中
A few things can cause this. 一些事情可能会导致这种情况。 First thing to try is to make sure you have a JPA Facet correctly setup:
首先要尝试确保正确设置JPA Facet:
Go to Project Structure, select Modules and check your module in the list has a JPA facet. 转到项目结构,选择模块并检查列表中的模块是否具有JPA方面。 If it doesn't then add one.
如果没有,则添加一个。
If that alone doesn't help, then also try looking at your Spring config. 如果仅此一点没有帮助,那么也尝试查看你的Spring配置。 You've show that the package(s) to scan for entities is com.tarameshgroup.derakht.repository
您已经表明要扫描实体的包是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.
我有IDEA问题,我有子包,虽然如果我定义顶级包,应用程序运行正常,但IDEA不会自动选择它们用于语法高亮显示。 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. 因此,举例来说,如果我有包
com.foo.bar.domain
在一些实体,以及com.foo.bar.domain.subpkg
和我的Spring配置有packagesToScan
的正义价值com.foo.bar.domain
然后该包中的实体将具有正确的突出显示,但不包括子包。 The answer was to change the Spring configured packagesToScan
value to include the sub-packages, even though Spring itself doesn't require this, ie com.foo.bar.domain, com.foo.bar.domain.subpkg
答案是更改Spring配置的
packagesToScan
值以包含子包,即使Spring本身不需要这个,即com.foo.bar.domain, com.foo.bar.domain.subpkg
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 (eg, c. C ontentCategory), not the field names (eg, c. c ontentCategory). 你有类型(例如,C。ÇontentCategory),而不是字段名(例如c。ÇontentCategory)。
c.contentCategory.genre
instead 代替
c.ContentCategory.genre
and 和
c.contentType.genre
instead 代替
c.ContentType.genre
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, eg using Hibernate create and/or create-drop . IntelliJ IDEA并不总是解析spring数据jpa @query注释中的实体,它在运行时解析它,因为你可以在应用程序启动时创建/修改实体,例如使用Hibernate create和/或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.
有一个IntelliJ设置,您需要正确设置,以免出现此错误 - 请参阅IDEA IntelliJ的文档。
You may need to add "Configure JPA or Hibernate facet" quick fix in IntelliJ. 您可能需要在IntelliJ中添加“配置JPA或Hibernate facet”快速修复。
Another possible solution can be using NamedQuery in the entity POJO . 另一种可能的解决方案是在实体POJO中使用NamedQuery 。 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();
Two solutions come to mind: 我想到了两种解决方案:
The second solution has fixed most of the problems I had with Intellij query highlighting. 第二个解决方案解决了我在Intellij查询突出显示时遇到的大部分问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.