<legend id='JAfq4'><style id='JAfq4'><dir id='JAfq4'><q id='JAfq4'></q></dir></style></legend>
  • <i id='JAfq4'><tr id='JAfq4'><dt id='JAfq4'><q id='JAfq4'><span id='JAfq4'><b id='JAfq4'><form id='JAfq4'><ins id='JAfq4'></ins><ul id='JAfq4'></ul><sub id='JAfq4'></sub></form><legend id='JAfq4'></legend><bdo id='JAfq4'><pre id='JAfq4'><center id='JAfq4'></center></pre></bdo></b><th id='JAfq4'></th></span></q></dt></tr></i><div id='JAfq4'><tfoot id='JAfq4'></tfoot><dl id='JAfq4'><fieldset id='JAfq4'></fieldset></dl></div>

      1. <tfoot id='JAfq4'></tfoot>
      2. <small id='JAfq4'></small><noframes id='JAfq4'>

        • <bdo id='JAfq4'></bdo><ul id='JAfq4'></ul>

        将 @EmbeddedId 与 JpaRepository 一起使用

        时间:2024-08-23

            1. <i id='784Z3'><tr id='784Z3'><dt id='784Z3'><q id='784Z3'><span id='784Z3'><b id='784Z3'><form id='784Z3'><ins id='784Z3'></ins><ul id='784Z3'></ul><sub id='784Z3'></sub></form><legend id='784Z3'></legend><bdo id='784Z3'><pre id='784Z3'><center id='784Z3'></center></pre></bdo></b><th id='784Z3'></th></span></q></dt></tr></i><div id='784Z3'><tfoot id='784Z3'></tfoot><dl id='784Z3'><fieldset id='784Z3'></fieldset></dl></div>

                <bdo id='784Z3'></bdo><ul id='784Z3'></ul>
                  <tbody id='784Z3'></tbody>
                <legend id='784Z3'><style id='784Z3'><dir id='784Z3'><q id='784Z3'></q></dir></style></legend><tfoot id='784Z3'></tfoot>

                  <small id='784Z3'></small><noframes id='784Z3'>

                  本文介绍了将 @EmbeddedId 与 JpaRepository 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个简单的 Entitly 类,其中包含 @EmbeddedId(IntegerString 字段在单独的类中).我使用 Spring Data (org.springframework.data.jpa.repository.JpaRepository) 访问数据库 (MySql),使用正常的 Id 查询工作正常,由 Spring 和我自己写的.使用 EmbeddedId 我没有设法创建正确的查询.我想要做的是选择所有 id(发生某些条件的 embeddedId 字段之一)这里有一些代码示例,也许有人会知道如何解决它.
                  实体类:

                  I have simple Entitly class with the @EmbeddedId (Integer and String fields in separate class). And I use the Spring Data (org.springframework.data.jpa.repository.JpaRepository) to access the database (MySql), with the normal Id the queries are working fine, both the generated by Spring and the ones wrote by myself. With the EmbeddedId I didnt manage to create the correct query. What I want to do is to select all the id (one of the fields of embeddedId for which some condition occurs) Here you have some code samples, maybe somebody will have an idea how to solve it.
                  The entity class:

                  @Entity
                  @Table(name="table_name")
                  public class EntityClass {
                  
                      @EmbeddedId
                      private EmbeddedIdClass id;
                      private String  someField;
                      //rest of implemetation
                  }
                  

                  EmbeddedId 类:

                  the EmbeddedId class:

                  @Embeddable
                  public class EmbeddedIdClass implements Serializable {
                  
                  public EmbeddedIdClass(Long id, String language) {
                      super();
                      this.id = id;
                      this.language = language;
                  }
                  
                  public UserAdTextId() {}        
                  
                  @Column(name="ad_id", nullable=false)
                      private Integer id;
                  
                      @Column(name="language_code", nullable=false)
                      private String  language;
                      //rest of implemetation
                  }
                  

                  和存储库:

                  @Transactional(readOnly=true)
                  public interface MyRepository extends JpaRepository<EntityClass, EmbeddedIdClass> {
                      @Query("select distinct ad_id from EntityClass where userId = :userId and (/*here the conditions*/)")
                      public Page<Integer> findUserAdsWithSearchString(@Param("userId") Integer userId, @Param("searchString") String searchString, Pageable page);
                  //rest of implemetation
                  }
                  

                  我没有找到任何文档如何创建支持 @EmbeddedId 的方法,我尝试了许多不同的方法名称,但我总是从方法解析器中得到异常..

                  I didn't find any documentation how to create the methods for supporting the @EmbeddedId, I was trying many different method names, but I always get exceptions from the method parser..

                  推荐答案

                  您的查询似乎使用了列名.它应该包含属性名称,包括嵌入对象的导航.这里还有一个相关的问题:How to write JPQL SELECT with embedded id?

                  It seems your query is using column names. It should contain the property names, including navigation into embedded objects. There's also a related question here on SO: How to write JPQL SELECT with embedded id?

                  select distinct id.id from EntityClass where userId = :userId and (...)
                  

                  第一个id是指EntityClass(类型为EmbeddedIdClass)的属性id,第二个是指EntityClass的属性idEmbeddedIdClassid 属性.

                  The first id refers to attribute id of EntityClass (of type EmbeddedIdClass), and the second one refers to the id property of EmbeddedIdClass.

                  另外,请确保 EntityClass 中有 userId 属性.

                  Also, make sure there's a userId property in EntityClass.

                  这篇关于将 @EmbeddedId 与 JpaRepository 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:QueryDsl - 集合表达式中的子查询 下一篇:获取 Spring Data 上按日期排序的最后记录

                  相关文章

                  <i id='vma0P'><tr id='vma0P'><dt id='vma0P'><q id='vma0P'><span id='vma0P'><b id='vma0P'><form id='vma0P'><ins id='vma0P'></ins><ul id='vma0P'></ul><sub id='vma0P'></sub></form><legend id='vma0P'></legend><bdo id='vma0P'><pre id='vma0P'><center id='vma0P'></center></pre></bdo></b><th id='vma0P'></th></span></q></dt></tr></i><div id='vma0P'><tfoot id='vma0P'></tfoot><dl id='vma0P'><fieldset id='vma0P'></fieldset></dl></div>

                  • <bdo id='vma0P'></bdo><ul id='vma0P'></ul>

                    <small id='vma0P'></small><noframes id='vma0P'>

                  1. <tfoot id='vma0P'></tfoot>

                    1. <legend id='vma0P'><style id='vma0P'><dir id='vma0P'><q id='vma0P'></q></dir></style></legend>