title: How to make a Jpql query work in Spring Jpa Repository with Optional return?
body: <p>I am using Jpql queries in my JpaRepository. The problem is that every time I call a method with an optional return I get this error:</p>
  <blockquote>
  <p>java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast
  to class com.netpos.pagvendas.campaignuser.entity.CampaignUser
  ([Ljava.lang.Object; is in module java.base of loader 'bootstrap';
  com.netpos.pagvendas.campaignuser.entity.CampaignUser is in unnamed
  module of loader
  org.springframework.boot.devtools.restart.classloader.RestartClassLoader
  @1c069e07)</p>
  </blockquote>
  <p>This is my Repository class, and the method that I am trying to use is :</p>
  <p><code>findByCampaignCodUniqueCampaignAndCodTenant(String codUniqueCampaign, String codTenant)</code></p>
  <pre><code>@Repository
  public interface CampaignUserRepository extends JpaRepository&lt;CampaignUser, CampaignUserPk&gt; {
  
      @Query(&quot;SELECT campaignUser, campaignUser.campaign from CampaignUser campaignUser &quot; +
              &quot;where campaignUser.codTenant = :codTenant &quot; +
              &quot;and campaignUser.flgFinished = false &quot; +
              &quot;and :now &gt;= campaignUser.campaign.datStart and :now &lt;= campaignUser.campaign.datEnd &quot; +
              &quot;order by campaignUser.datCreation ASC&quot;)
      List&lt;CampaignUser&gt; findActiveCampaign(String codTenant, ZonedDateTime now);
  
      @Query(&quot;SELECT campaignUser, campaignUser.campaign from CampaignUser campaignUser &quot; +
              &quot;where campaignUser.idtCampaign = :idtCampaign &quot;)
      List&lt;CampaignUser&gt; findByIdtCampaign(long idtCampaign);
  
      @Query(&quot;SELECT campaignUser, campaignUser.campaign from CampaignUser campaignUser &quot; +
              &quot;where campaignUser.campaign.codUniqueCampaign = :codUniqueCampaign &quot; +
              &quot;and campaignUser.codTenant = :codTenant&quot;)
      Optional&lt;CampaignUser&gt; findByCampaignCodUniqueCampaignAndCodTenant(String codUniqueCampaign, String codTenant);
  }
  </code></pre>
  <p>Is it possible to use Optional with Jpql queries in JpaRepository?</p>