[英]How to get a HashMap result from Spring Data Repository using JPQL?
I've currently implemented a Ternary relationship using Map between User , Shop and Role .我目前已经在User 、 Shop和Role之间使用 Map 实现了三元关系。 So in my User Entity i have this mapping:所以在我的用户实体中我有这个映射:
User.java用户.java
@Entity
@Table(name = "us_users")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
private int id;
@Basic(fetch = FetchType.LAZY, optional = false)
private String uuid;
@NotBlank(message = "First name can not be empty")
@Column(name = "usFname")
private String usFname;
@NotBlank(message = "Username can not be left empty")
@Column(name = "us_lname")
private String usLname;
@NotBlank(message = "Email field can not be empty")
@ValidEmail
@Column(name = "usEmail", unique = true)
@Basic(fetch = FetchType.LAZY, optional = false)
private String usEmail;
//this is the actual mapping
//a user can have a list of shops, but one role assigned for each shop
@ManyToMany
@MapKeyJoinColumn(name = "shop_fk")
@JoinTable(name = "user_shop_role",
joinColumns = @JoinColumn(name = "user_fk"), inverseJoinColumns = @JoinColumn(name = "role_fk"))
private Map<Shop, Role> shopRoleMap = new HashMap<>();
//GETTERS AND SETTERS,
}
Role.java角色.java
@Entity
@Table(name = "ro_roles")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
private int id;
@Column(name = "rn_role_name")
private String roleName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
} }
Shop.java店铺.java
@Entity
@Table(name = "sh_shops")
public class Shop {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(nullable = false)
private int id;
@NotBlank(message = "Shop name can not be left blank")
private String sh_name;
@NotBlank(message = "please provide shop icon")
private String sh_icon;
@NotBlank(message = "please fill the shop description")
private String sh_description;
@NotBlank
private String sh_tag;
//setters, getters, equals, hashCode methods.
}
This mapping gives me another table ( user_shop_role ) in the database that has user_fk , shop_fk and role_fk which is lovely.此映射为我提供了数据库中的另一个表 ( user_shop_role ),其中包含user_fk 、 shop_fk和role_fk ,这很可爱。 The difficult bit is to make a Query using Spring Data JPA and get the result as a HashMap for a given user as this: HashMap<Shop,Role> for the logged in user.困难的一点是使用 Spring 数据 JPA 进行查询,并为给定用户获得 HashMap 的结果:登录用户的 HashMap<Shop,Role>。
At the UserRepository layer i have tried this:在 UserRepository 层我试过这个:
interface UserRepository extends JpaRepository and i have this query...接口UserRepository扩展了 JpaRepository 并且我有这个查询......
@Query("SELECT u FROM User u join shopRoleMap m where key(m)= usEmail")
public HashMap<Shop, Role> findByUser(String email);
}
How can i get results for a ternary relationship mapped using a Map Collection to get a list of shops and corresponding role value.我如何获得使用 Map 集合映射的三元关系的结果,以获取商店列表和相应的角色值。 More of a key,value result?更多的关键,价值结果?
@Repository
public interface UserRepository
extends JpaRepository<User, Integer> {
@Query("SELECT key(m), value(m) FROM User u join u.shopRoleMap m" +
" where u.usEmail = :email")
List<Object[]> findByUserEmail(String email);
}
select
shop3_.id as id1_1_0_,
role2_.id as id1_0_1_,
shop3_.sh_description as sh_descr2_1_0_,
shop3_.sh_icon as sh_icon3_1_0_,
shop3_.sh_name as sh_name4_1_0_,
shop3_.sh_tag as sh_tag5_1_0_,
role2_.rn_role_name as rn_role_2_0_1_
from
us_users user0_
inner join
user_shop_role shoprolema1_
on user0_.id=shoprolema1_.user_fk
inner join
ro_roles role2_
on shoprolema1_.role_fk=role2_.id
inner join
sh_shops shop3_
on shoprolema1_.shop_fk=shop3_.id
where
user0_.us_email=?
Update with above code plus another way用上面的代码加上另一种方式更新
Github repo - https://github.com/kavi-kanap/stackoverflow-6313585 Github 回购 - https://github.com/kavi-kanap/stackoverflow-6313585
Spring Data JPA has a feature called Property Expressions where you can access the child or related entity Spring 数据 JPA 具有称为属性表达式的功能,您可以在其中访问子实体或相关实体
For example例如
List<Shop> findByUser_email(String email);
For more details refer to this有关更多详细信息,请参阅此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.