简体   繁体   中英

Using named query inside entity method in Hibernate

I have an entity that has a @ManyToMany collection marked as lazy. When I list those entities in a JSP I call a method in each entity to decide if I should show a button or not, that method was developed to count the number of elements in that collection, so if the number of elements is too high it takes forever to show the JSP because Hibernate loads the entire collection with all of their data.

I was wondering if there's a way to call a NamedQuery from that method in the entity, for example:

@NamedNativeQuery(
    name="showButton",
    query="SELECT count(distinct(id)) FROM USER where GROUP_ID = :groupId")
@Entity 
class Group {
    ...

    @ManyToMany(mappedBy = "groups", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
        private Set<User> users = new HashSet<User>();
    }

    ...
    public boolean showButton() {
        // Can I call the named query here??
    }
}

@NamedQuery是不可能的,但是@Formula可用于计算属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM