org.hibernate.query.sqm.UnknownEntityException:无法解析根实体“api_key”

问题描述 投票:0回答:1

我正在使用

spring-boot
spring-data-jpa
。编写自定义 SQL 查询时遇到问题。 这是我的模型和存储库...

用户模型

@Entity
public class User {
  @Id @NotBlank private String userName;

  // TODO: store hashed password
  @Column(nullable = false)
  @NotBlank
  private String password;

  @OneToMany(fetch = FetchType.LAZY, targetEntity = Event.class, cascade = CascadeType.ALL)
  List<Event> events;
}

活动模型

@Entity
public class Event {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long eventId;

  @NotBlank private String eventName;
  @OneToMany(targetEntity = Dimension.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  @NotEmpty
  private List<Dimension> dimensions;

  @OneToMany(targetEntity = ApiKey.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  private List<ApiKey> apiKeys;
}

ApiKey 模型

@Entity
public class ApiKey {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  private String apiKey;
  private long expiryTime;
}

用户存储库

@Repository
public interface UserRepository extends CrudRepository<User, String> {
  @Query("""
    SELECT user_events.user_user_name AS user_name,
           event_api_keys.event_event_id AS event_id,
           api_key.api_key,
           api_key.expiry_time
    FROM api_key
    JOIN event_api_keys ON api_key.id = event_api_keys.api_keys_id
    JOIN user_events ON user_events.events_event_id = event_api_keys.event_event_id
    WHERE api_key='51018598c935462189a4941d57388456'
    """)
  List<ApiKeyMeta> fetchApiKeyMeta(@Param("apiKey") String apiKey);
}

生成的 SQL 架构

mysql> describe user;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| user_name | varchar(255) | NO   | PRI | NULL    |       |
| password  | varchar(255) | NO   |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+


mysql> describe event;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| event_id   | bigint       | NO   | PRI | NULL    |       |
| event_name | varchar(255) | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+


mysql> describe user_events;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| user_user_name  | varchar(255) | NO   | MUL | NULL    |       |
| events_event_id | bigint       | NO   | PRI | NULL    |       |
+-----------------+--------------+------+-----+---------+-------+


mysql> describe event_api_keys;
+----------------+--------+------+-----+---------+-------+
| Field          | Type   | Null | Key | Default | Extra |
+----------------+--------+------+-----+---------+-------+
| event_event_id | bigint | NO   | MUL | NULL    |       |
| api_keys_id    | bigint | NO   | PRI | NULL    |       |
+----------------+--------+------+-----+---------+-------+


mysql> describe api_key;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id          | bigint       | NO   | PRI | NULL    |       |
| api_key     | varchar(255) | YES  |     | NULL    |       |
| expiry_time | bigint       | NO   |     | NULL    |       |
+-------------+--------------+------+-----+---------+-------+

执行

fetchApiKeyMeta
会抛出以下异常。

Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.watchman.metaserver.user.repository.UserRepository.fetchApiKeyMeta(java.lang.String); Reason: Validation failed for query for method public abstract java.util.List com.watchman.metaserver.user.repository.UserRepository.fetchApiKeyMeta(java.lang.String)




Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.watchman.metaserver.user.repository.UserRepository.fetchApiKeyMeta(java.lang.String)


Caused by: java.lang.IllegalArgumentException: org.hibernate.query.sqm.UnknownEntityException: Could not resolve root entity 'api_key'



Caused by: org.hibernate.query.sqm.UnknownEntityException: Could not resolve root entity 'api_key'

无法弄清楚问题出在哪里,有人可以帮助我吗?

java mysql spring-boot hibernate
1个回答
0
投票

您的查询不是原生查询,因此您应该引用实体名称而不是表名称。

例如:FROM api_key 应该是 FROM ApiKey(实体)

对于其他表也是如此。

否则添加nativeQuery=true并像普通Sql查询一样使用它

© www.soinside.com 2019 - 2024. All rights reserved.