Java – use the distinct keyword for each column in JPA

I'm reading some values from a very standardized database (I can't control it) This call retrieves the announcements of university departments. If users are in multiple departments (possible), the same results will be returned to these users multiple times However, some departments may have different announcements, while others have the same announcement

Is there any way to use the distinct keyword in JPA on a separate column? This is what I currently use to query:

String jpql = "SELECT DISTINCT annoucement FROM Announcment announcement "
                + "WHERE (announcement.date <= :Now AND announcement.endDate >= :Now) "
                + "AND announcement.approved = true AND announcement.departmentId IN (:departmentIDs)";

TypedQuery<Announcement> query = entityManager.createQuery(jpql,Announcement.class);
query.setParameter("Now",new Date());
query.setParameter("departmentIDs",departmentIDs);

The departmentid value may be different, but the announcement, date, etc. are the same This query returns announcements with duplicate values

Solution

I ask two questions:

>"Select explicit announcement. X, announcement. Y, announcement. Z... (no depid)“

Then build the announcement But you lose the persistent object and its references You must load them and your Dao object will appear again if you need it. > Override equals () [hashcode ()) in the announcement class. Of course, in equals (), depid should not be in the comparison. More and more, as you do, then convert the list to set. You get "different" objects

I hope I can help you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>