java - 将 @EmbeddedId 与 JpaRepository 一起使用

标签 java spring jpa jpql spring-data

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

@Entity
@Table(name="table_name")
public class EntityClass {

    @EmbeddedId
    private EmbeddedIdClass id;
    private String  someField;
    //rest of implemetation
}

EmbeddedId 类:

@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 的方法,我尝试了许多不同的方法名称,但我总是从方法解析器中得到异常。

最佳答案

(约西·列夫) 这可以通过以下方式完成: 假设您的主要实体是:

@Entity
@Table(name="JRULES_FLOW")
public class JrulesFlow implements Serializable {
   private static final long serialVersionUID = 1L;

   @EmbeddedId
   private JrulesFlowPK id;

   @Column(name="NEXT_SEQ")
   private int nextSeq;

   @Column(name="REF_ID")
   private String refId;

   @Column(name="TASK_TYPE")
   private String taskType;

   @Column(name="VALUE_TO_FIND")
   private String valueToFind;
}

你的PK课是:

@Embeddable
public class JrulesFlowPK implements Serializable {
   //default serial version id, required for serializable classes.
   private static final long serialVersionUID = 1L;

   @Column(name="FLOW_ID")
   private String flowId;

   @Column(name="TASK_SEQ")
   private long taskSeq;
 }

JPA 存储库方法名称应包含 id 字段的名称 主类,后跟要在 PK 类中查询的属性:

public interface JrulesFlowRepository extends JpaRepository<JrulesFlow, 
      JrulesFlowPK> { // NOTE: put here both classes - also the pk class..
   public List<JrulesFlow>  findByIdFlowId(String flowId);  // Id - is the 
                  // @EmbeddedId in JrulesFlow. FlowId is an attribute 
                  // within JrulesFlowPK
}

关于java - 将 @EmbeddedId 与 JpaRepository 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10649691/

相关文章:

java - 正则表达式查找整个单词

java - 如何为所有实体定义 allocationSize 和 initialValue

java - 如何避免使用jpa2 criteriaBuilder.like中的输入 "%"

java - 理解如何创建实体管理器或者我是否可以在 JAVA EE 中生成它们的问题

java - Android 中平铺 Sprite

Java 文件有时有效有时无效

java - 在我的加密字符串上附加新行

spring - DefaultSubscriptionRegistry不适应高并发和异常情况

java - 没有为 OPTIONS/DELETE 正确启用 Spring Boot Data Rest + CORS

java - 过滤器不适用于一对多关系的 JPQL 查询