2012-12-02 42 views
5

Tôi có hai thực thể:Làm thế nào để "đặt hàng bằng chức năng tổng hợp" trong dữ liệu mùa xuân?

ResourceFile:

@Entity 
@Table(name = "resource_file") 
public class ResourceFile extends IdEntity<Integer> { 

    @Id 
    @SequenceGenerator(name = "resource_file_id_generator", sequenceName = "resource_file_id", allocationSize = 1) 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "resource_file_id_generator") 
    @Column(name = "id", unique = true, nullable = false) 
    @Nonnegative 
    private Integer id; 

    ... 
} 

FavoriteResourceFile:

@Entity 
@Table(name = "favorite_resource_file") 
@Cacheable 
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
public class FavoriteResourceFile extends IdEntity<FavoriteResourceFileId> { 

    @EmbeddedId 
    private FavoriteResourceFileId id; 

    @MapsId("resourceFileId") 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "resource_file_id", nullable = false) 
    private ResourceFile resourceFile; 

    ... 

} 

Và tôi muốn thực hiện các truy vấn sau "chọn tất cả các file tài nguyên và sắp xếp chúng theo số lượng yêu thích tập tin tài nguyên của ".

Trong SQL nó trông giống như:

select rf.id, count(frf.resource_file_id) from resource_file rf 
left join favorite_resource_file frf on frf.resource_file_id = rf.id 
group by rf.id 
order by count(rf.id) desc; 

Nhưng tôi không thể hiểu được làm thế nào để làm điều đó với dữ liệu mùa xuân và làm thế nào để làm cho bản đồ để ResourceFile thực ở cuối.

Một số hạn chế:

  • tôi không thể làm cho mối quan hệ với FavoriteResourceFile trong ResourceFile, vì họ đang nằm trong module khác nhau
  • Tôi không muốn sử dụng SQL mẹ đẻ hoặc JPA truy vấn (như dây đàn).
  • Sẽ thích hợp hơn khi sử dụng Mô hình, Đặc điểm kỹ thuật hoặc QueryDSL, vì chúng đã được sử dụng trong dự án.

Ai đó có thể giúp tôi không?

Trả lời

6

Bạn có thể sử dụng thực hiện kho lưu trữ tùy chỉnh của bạn cùng với CRUD/PagingAndSorting thực hiện kho, như thế này:

End-point repo:

public interface ResourceFileRepository extends 
    PagingAndSortingRepository<ResourceFile, Integer>, 
    ResourceFileRepositoryCustom { 
} 

Tuỳ chỉnh repo:

public interface ResourceFileRepositoryCustom { 
    List<ResourceFile> getResourceFilesOrderByFavourites(); 
} 

Tuỳ chỉnh repo thực hiện với mã thực tế để retrive ResourceFile được sắp xếp theo số yêu thích (thông báo nó là ResourceFileRepositoryImpl chứ không phải R esourceFileRepositoryCustomImpl).

Tôi không có các khóa được nhúng đó, vì vậy tôi phải đơn giản hóa nó một chút. Truy vấn sẽ TỪ FavoriteResourceFile, vì ResourceFile không có mối quan hệ riêng với FavoriteResourceFile.

public class ResourceFileRepositoryImpl implements ResourceFileRepositoryCustom { 
    @PersistenceContext 
    private EntityManager em; 

    public void setEntityManager(EntityManager em) { 
     this.em = em; 
    } 

    @Override 
    public List<ResourceFile> getResourceFilesOrderByFavourites() { 
     CriteriaBuilder criteriaBuilder = this.em.getCriteriaBuilder(); 
     CriteriaQuery<ResourceFile> q = criteriaBuilder 
       .createQuery(ResourceFile.class); 
     Root<FavoriteResourceFile> root = q.from(FavoriteResourceFile.class); 
     Join<FavoriteResourceFile, ResourceFile> join = root.join(
       FavoriteResourceFile_.resourceFile, JoinType.LEFT); 
     q.select(join); 
     q.groupBy(join.get(ResourceFile_.id)); 
     q.orderBy(criteriaBuilder.desc(
         criteriaBuilder.count(
          join.get(ResourceFile_.id)))); 

     TypedQuery<ResourceFile> query = this.em.createQuery(q); 
     return query.getResultList(); 
    } 
} 

Để xem dự án ví dụ đầy đủ (với một số sql rất cơ bản và thử nghiệm) - thanh toán/nĩa/etc: https://github.com/rchukh/StackOverflowTests/tree/master/13669324

Các vấn đề liên quan