how to return Map with HQL

后端 未结 5 804
故里飘歌
故里飘歌 2020-11-28 08:56

i have a table

Permission:

  • id
  • name
  • desc

what i am doing right now is to make a query that returns a p

5条回答
  •  孤街浪徒
    2020-11-28 09:12

    1. Use the select new map syntax in HQL to fetch the results of each row in a Map. Take a look at the following question, that addresses the issue: How to fetch hibernate query result as associative array of list or hashmap. For instance, the following HQL: select new map(perm.id as pid, perm.name as pname) from Permission perm will return a List of Maps, each one with keys "pid" and "pname".

    2. It is not possible to map an association to a Map. It is possible to map the key of the Map to a column with the @MapKeyColumn annotation in the association. See this question, that also addresses the issue, for an example: JPA 2.0 Hibernate @OneToMany + @MapKeyJoinColumn. Here is another example.

    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(name = "perm_cat_map", 
        joinColumns = { @JoinColumn(name = "perm_cat_id") }, 
        inverseJoinColumns = { @JoinColumn(name = "permission_id") })
    @MapKeyColumn(name="permission_id")
    private Map permissions = new HashMap(0);
    
    

提交回复
热议问题