[英][Spring boot ]Error in native query with join spring data jpa
I have a many to many relationship in spring boot with User and Role.我在 spring 引导中与用户和角色有多对多的关系。 I have three tables in database (Role_utilisateur, users_roles and utilisateurs).
我在数据库中有三个表(Role_utilisateur、users_roles 和 utilisateurs)。 In my repository, ireturn List of my entite called RoleEntite which have two attributes (nom and id).
在我的存储库中,ireturn 名为 RoleEntite 的实体列表,它有两个属性(nom 和 id)。
@Query(value = "SELECT ROLE_UTILISATEUR.NOM, ROLE_UTILISATEUR.ROLE_ID " + "FROM ROLE_UTILISATEUR "
+ "INNER JOIN USERS_ROLES on users_roles.role_id = ROLE_UTILISATEUR.ROLE_ID "
+ "INNER JOIN UTILISATEURS on utilisateurs.utilisateur_id = USERS_ROLES.UTILISATEUR_ID "
+ "WHERE UTILISATEURS.UTILISATEUR_ID = :userId", nativeQuery = true)
List<RoleEntite> findUsersRolesNative(@Param("userId") Long userId);
When i call my function in当我打电话给我的 function
private List<GrantedAuthority> getGrantedAuthorities(UserEntite user) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
List<RoleEntite> roleList = userRepository.findUsersRolesNative(user.getId());
for (RoleEntite roleEntite : roleList) {
authorities.add(new SimpleGrantedAuthority(roleEntite.getNom()));
}
return authorities;
}
I get this error in my java eclipse我在 java eclipse 中收到此错误
org.springframework.security.authentication.InternalAuthenticationServiceException: Failed to convert from type [java.lang.Object[]] to type [com.id.firstSpringBoot.entite.RoleEntite] for value '{ADMIN, 1}'; org.springframework.security.authentication.InternalAuthenticationServiceException:无法从类型 [java.lang.Object[]] 转换为类型 [com.id.firstSpringBoot.entite.RoleEntite] 的值'{ADMIN,1}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [com.id.firstSpringBoot.entite.RoleEntite]
嵌套异常是 org.springframework.core.convert.ConverterNotFoundException:找不到能够从类型 [java.lang.String] 转换为类型 [com.id.firstSpringBoot.entite.RoleEntite] 的转换器
How can i resolve this error and get the name ADMIN in my function getGrantedAuthorities can do this如何解决此错误并在我的 function getGrantedAuthorities 中获取名称 ADMIN 可以做到这一点
The problem is you're selecting two columns and returning a list of entities.问题是您选择了两列并返回实体列表。 You need to either select the entity in your query or return the list of two values in a collection.
您需要 select 查询中的实体或返回集合中两个值的列表。
To return entities you would need to convert your query to JPQL.要返回实体,您需要将查询转换为 JPQL。 The following is a JPQL representation of your native query.
以下是您的本机查询的 JPQL 表示。 I've had to do some guessing for the joins (I assume you have some JPA related entities):
我不得不对连接做一些猜测(我假设你有一些 JPA 相关实体):
@Query(value = "SELECT RoleEntite r "
+ "INNER JOIN UserRole r.user u "
+ "INNER JOIN Utilisateurs u.Utilisateur x "
+ "WHERE x.UTILISATEUR_ID = :userId")
List<RoleEntite> findUsersRolesNative(@Param("userId") Long userId);
If you go the native query route:如果你 go 原生查询路由:
@Query(value = "SELECT ROLE_UTILISATEUR.NOM, ROLE_UTILISATEUR.ROLE_ID " + "FROM ROLE_UTILISATEUR "
+ "INNER JOIN USERS_ROLES on users_roles.role_id = ROLE_UTILISATEUR.ROLE_ID "
+ "INNER JOIN UTILISATEURS on utilisateurs.utilisateur_id = USERS_ROLES.UTILISATEUR_ID "
+ "WHERE UTILISATEURS.UTILISATEUR_ID = :userId", nativeQuery = true)
List<Object[]> findUsersRolesNative(@Param("userId") Long userId);
The returned list should yield:返回的列表应产生:
for (Object[] obj : list) {
nom = obj[0];
roleId = obj[1];
// ... do what ever you want with the values
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.