2012-05-18 11 views
20

Tôi có lớp Entitly đơn giản với các trường @EmbeddedId (IntegerString trong lớp riêng biệt). Và tôi sử dụng dữ liệu Spring (org.springframework.data.jpa.repository.JpaRepository) để truy cập cơ sở dữ liệu (MySql), với Id bình thường các truy vấn đang hoạt động tốt, cả hai được tạo ra bởi Spring và những người thân đã viết. Với số EmbeddedId tôi đã không quản lý để tạo truy vấn chính xác. Những gì tôi muốn làm là chọn tất cả các id (một trong các lĩnh vực của embeddedId mà một số điều kiện xảy ra) Ở đây bạn có một số mẫu mã, có lẽ ai đó sẽ có một ý tưởng làm thế nào để giải quyết nó.
Các lớp thực thể:Sử dụng @EmbeddedId với JpaRepository

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

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

lớp 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 
} 

và kho:

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

tôi không tìm thấy bất kỳ tài liệu như thế nào để tạo ra các phương pháp hỗ trợ @EmbeddedId, tôi đã thử nhiều tên phương thức khác nhau, nhưng tôi luôn nhận được ngoại lệ từ trình phân tích cú pháp phương pháp.

Trả lời

10

Dường như truy vấn của bạn đang sử dụng tên cột. Nó phải chứa các tên thuộc tính, bao gồm cả điều hướng vào các đối tượng nhúng. Ngoài ra còn có một câu hỏi liên quan ở đây trên SO: How to write JPQL SELECT with embedded id?

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

Các id đầu tiên đề cập đến thuộc tính id của EntityClass (loại EmbeddedIdClass), và một trong những thứ hai đề cập đến id tài sản của EmbeddedIdClass.

Ngoài ra, hãy đảm bảo có thuộc tính userId trong EntityClass.

+0

Đúng vậy, tôi phải sử dụng tên trường và duyệt qua chúng, tuy nhiên tôi nhận được kết xuất sql với số đếm seelct (khác biệt ...) Số đếm từ đâu? kiểu trả về là Page hoặc Danh sách (Tôi đã thử cả hai ... – Mat

+0

Câu trả lời là ok, một vấn đề khác mà tôi có là có thể xem được ... có vẻ như các truy vấn có phân biệt hoặc nhóm không thể xử lý được ... :( – Mat

21

(bởi yosi Lev) Điều này có thể được thực hiện như sau: Giả sử thực thể chính của bạn là:

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

Và lớp PK của bạn là:

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

Các JPA kho tên phương pháp shouls bao gồm tên của trường id trong lớp chính theo sau là thuộc tính bạn muốn truy vấn uppon trong lớp 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 
} 
+1

cho nhiều cột-- findByIdCol1AndIdCol2AndIdCol3 (Chuỗi col1, Chuỗi col2, Chuỗi col3); hoạt động – Boo