<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xmlns:tt="http://teletype.in/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>Alexander</title><generator>teletype.in</generator><description><![CDATA[Записки новичка по Java и всему, что с ним связано]]></description><link>https://teletype.in/@eska2000?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=eska2000</link><atom:link rel="self" type="application/rss+xml" href="https://teletype.in/rss/eska2000?offset=0"></atom:link><atom:link rel="next" type="application/rss+xml" href="https://teletype.in/rss/eska2000?offset=10"></atom:link><atom:link rel="search" type="application/opensearchdescription+xml" title="Teletype" href="https://teletype.in/opensearch.xml"></atom:link><pubDate>Mon, 25 May 2026 19:08:42 GMT</pubDate><lastBuildDate>Mon, 25 May 2026 19:08:42 GMT</lastBuildDate><item><guid isPermaLink="true">https://teletype.in/@eska2000/wr3cXW22XfO</guid><link>https://teletype.in/@eska2000/wr3cXW22XfO?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=eska2000</link><comments>https://teletype.in/@eska2000/wr3cXW22XfO?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=eska2000#comments</comments><dc:creator>eska2000</dc:creator><title>Spring Data, Hibernate: Пересечение множеств и запрос во вложенные параметры</title><pubDate>Sun, 15 Aug 2021 13:48:41 GMT</pubDate><description><![CDATA[Есть сущности (показаны базовые поля):]]></description><content:encoded><![CDATA[
  <p>Есть сущности (показаны базовые поля):</p>
  <p>ApplicationUser</p>
  <pre data-lang="java">@Entity
@Table(name = &quot;application_user&quot;)
@Data
public class ApplicationUser {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;sequenceGenerator&quot;)
    @SequenceGenerator(name = &quot;sequenceGenerator&quot;)
    private Long id;

    @Column(name = &quot;is_active&quot;)
    private Boolean isActive;

    @Column(name = &quot;second_name&quot;)
    private String secondName;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(unique = true)
    private User user;

}</pre>
  <p>User</p>
  <pre data-lang="java">@Entity
@Table(name = &quot;sys_user&quot;)
@Data
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;sequenceGenerator&quot;)
    @SequenceGenerator(name = &quot;sequenceGenerator&quot;)
    private Long id;

    @NotNull
    @Email
    @Size(min = 5, max = 254)
    @Column(name = &quot;email&quot;, length = 254, unique = true, nullable = false)
    private String email;

    @ManyToMany
    @JoinTable(
            name = &quot;sys_user_authority&quot;,
            joinColumns = { @JoinColumn(name = &quot;user_id&quot;, referencedColumnName = &quot;id&quot;) },
            inverseJoinColumns = { @JoinColumn(name = &quot;authority_name&quot;, referencedColumnName = &quot;name&quot;) }
    )
    @BatchSize(size = 20)
    private Set&lt;Authority&gt; authorities = new HashSet&lt;&gt;();

}</pre>
  <p>Authority</p>
  <pre data-lang="java">@Entity
@Table(name = &quot;authority&quot;)
@Data
public class Authority {

    @NotNull
    @Size(max = 50)
    @Id
    @Column(length = 50)
    private String name;

}</pre>
  <p>Требуется вывести всех активных пользователей (ApplicationUser) в режиме Pageable, которые принадлежат определённым ролям (Authority).</p>
  <p>Первый вариант:</p>
  <pre data-lang="java">Page&lt;ApplicationUser&gt; findAllByIsActiveIsTrueAndUser_AuthoritiesIsIn(Set&lt;Authority&gt; authorities, Pageable pageable);</pre>
  <p>Работает, но выводятся дубли (по кол-ву совпавших у пользователя ролей).</p>
  <p>Второй вариант:</p>
  <pre data-lang="java">@Query(
        value = &quot;select distinct applicationUser from ApplicationUser applicationUser left join fetch applicationUser.user, Authority a &quot; +
                &quot;where a in (:authorities) and a in elements(applicationUser.user.authorities) and applicationUser.isActive = true&quot;,
        countQuery = &quot;select distinct applicationUser from ApplicationUser applicationUser, Authority a &quot; +
                &quot;where a in (:authorities) and a in elements(applicationUser.user.authorities) and applicationUser.isActive = true&quot;
)
Page&lt;ApplicationUser&gt; findAllByIsActiveIsTrueAndUser_AuthoritiesIsIn(Set&lt;Authority&gt; authorities, Pageable pageable);</pre>
  <p>Всё работает. Для сравнения множеств в раздел from дополнительно указываем Authority, а в where сравниваем authority с искомым списком ролей, а затем с ролями пользователя и делаем distinct.</p>
  <p>Вариант findDistinctByIsActiveIsTrueAndUser_AuthoritiesIsIn для вложенных запросов не сработал, но должен работать для обычных запросов.</p>

]]></content:encoded></item><item><guid isPermaLink="true">https://teletype.in/@eska2000/mfDlzurQB_T</guid><link>https://teletype.in/@eska2000/mfDlzurQB_T?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=eska2000</link><comments>https://teletype.in/@eska2000/mfDlzurQB_T?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=eska2000#comments</comments><dc:creator>eska2000</dc:creator><title>Полезные атрибуты Spring Data</title><pubDate>Sun, 15 Aug 2021 13:33:56 GMT</pubDate><description><![CDATA[@CreatedDate - атрибут над полем столбца сущности с типом Date (java.util.Date)]]></description><content:encoded><![CDATA[
  <p><strong>@CreatedDate</strong> - атрибут над полем столбца сущности с типом Date (java.util.Date)</p>
  <p><strong>@LastModifiedDate</strong> - атрибут над полем столбца сущности с типом Date (java.util.Date)</p>
  <p><strong>@Enumerated(EnumType.STRING)</strong> - атрибут над полем столбца сущности с типом Enum</p>
  <p><strong>@MappedSuperClass</strong> - атрибут над базовым классом сущности</p>

]]></content:encoded></item></channel></rss>