40

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
}

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
}

and the repository:

@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
}

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..

4

2 回答 2

45

(由 Yosi Lev 撰写)这可以通过以下方式完成:假设您的主要实体是:

@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
}
于 2015-02-05T14:21:33.453 回答
19

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

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

第一个id是指(类型)的属性id,第二个是指的属性。EntityClassEmbeddedIdClassidEmbeddedIdClass

另外,请确保userId.EntityClass

于 2012-05-18T09:18:49.193 回答