如何获得投影中的实体

时间:2018-09-17 04:09:42

标签: spring-boot spring-data-jpa spring-data

我有实体:

@Entity
@Table(name = "story", schema = "")
@Data
public class Story implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "sID", unique = true, nullable = false)
    private Long sID;
    @Column(name = "vnName", nullable = false)
    private String vnName;
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss")
    @Column(name = "sUpdate", length = 19)
    private Date sUpdate;
}

和投影界面:

public interface NewStory {
    Story getStory();    
    Long getCnt();      
}

和存储库:

public interface StoryRepository extends CrudRepository<Story, Long> {

    @Query(value = "SELECT s.* , COUNT(u.chID) as Cnt"
            + " FROM Story s" 
            + " LEFT JOIN Chapter c ON s.sID = c.sID"
            + " LEFT JOIN _ufavorites u ON c.chID = u.chID"
            + " GROUP BY s.sID" + " ORDER BY COUNT(u.chID) DESC", 
            nativeQuery = true)
    public List<NewStory> getStoryNew();

}

结果:

[
    {
        "story": null,
        "cnt": 7
    },...
]

还是可以用其他方式完成?

0 个答案:

没有答案