【问题标题】:Using named query in orm.xml, getting error No identifier specified for entity在 orm.xml 中使用命名查询,得到错误 No identifier specified for entity
【发布时间】:2016-03-24 19:49:57
【问题描述】:

我正在尝试将我的命名查询存储在我的应用程序之外。到目前为止,我读到的所有内容都说将查询放在 orm.xml 中。所以我这样做了,就像这样:

   <?xml version="1.0" encoding="UTF-8"?>
...   
    <entity class="com.blah.mapdb.repository.ResultRepository">
        <table name="ResultRepository.getMapForTarget" />
        <named-query name="getMapForTarget">
        <query>
                <![CDATA[
                Big giant query goes here
                </query>
        </named-query>
    </entity>
    </entity-mappings> 

但是,当我尝试启动我的应用程序时,我收到了错误消息。挖掘堆栈跟踪会给我这个错误:

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.blah.mapdb.repository.ResultRepository

这是我的存储库类:

package com.blah.mapdb.repository;
import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import com.blah.mapdb.model.*;

public interface ResultRepository extends CrudRepository<Result, Long> {
      @Query(name="getMapForTarget",nativeQuery=true)
    List<Result> getMapForTarget (@Param("p1") String d, @Param("p2") String t, @Param("p3") String c);
...
}

这里是 Result 类的相关位:

package com.blah.mapdb.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Result {

    @Id
    private int id;
...
}

显然,结果类定义了一个 Id。如果我在 ResultRepository 类中包含查询,则一切正常。我不知道为什么我会收到关于 ResultRepository 类的错误。我在这里错过了什么?

【问题讨论】:

    标签: jpa spring-data spring-data-jpa named-query


    【解决方案1】:

    您的orm.xml 中有这一行:

    <entity class="com.blah.mapdb.repository.ResultRepository">
    

    这似乎是错误的,你应该有

    <entity class="com.blah.mapdb.model.Result"> 
    

    相反。此外,您的表名定义可能是错误的,您应该指定您的 Result 实体映射到的实际表名。

    【讨论】:

      猜你喜欢
      • 2018-07-10
      • 2011-01-23
      • 2021-06-13
      • 2021-10-18
      • 2013-09-16
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多