1. 在数据库中使用本机 SQL 创建视图,
create or replace view hunters_summary as
select
em.id as emp_id, hh.id as hh_id
from employee em
inner join employee_type et on em.employee_type_id = et.id
inner join head_hunter hh on hh.id = em.head_hunter_id;
2.将其映射到“不可变实体”
package inc.manpower.domain;
import org.hibernate.annotations.Immutable;
import org.hibernate.annotations.Subselect;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
@Entity
@Immutable
@Table(name = "`hunters_summary`")
@Subselect("select uuid() as id, hs.* from hunters_summary hs")
public class HuntersSummary implements Serializable {
@Id
private String id;
private Long empId;
private String hhId;
...
}
3. 现在使用所需的方法创建存储库,
package inc.manpower.repository;
import inc.manpower.domain.HuntersSummary;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
@Repository
@Transactional
public interface HuntersSummaryRepository extends PagingAndSortingRepository<HuntersSummary, String> {
List<HuntersSummary> findByEmpRecruitedDateBetweenAndHhId(Date startDate, Date endDate, String hhId);
}