繁体   English   中英

如何在 spring-data-jpa 中使用投影?

[英]How use projections in spring-data-jpa?

我需要在一个请求中获取关于票的所有信息,还有书名、作者和年份。 我已经实现了这个:我创建接口TicketWithBookView

public interface TicketWithBookView {
    Date getGiveAway();
    Long getReaderId();
    Date getTake();

    interface Book {
        String getAuthor();
        String getName();
        Integer getYearCreation();
    }
}

我的实体TicketEntity

@Data
@Entity
@Table(name = "ticket")
@NoArgsConstructor
@AllArgsConstructor
public class TicketEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(nullable = false)
    private Long readerId;
    @Column(nullable = false)
    private Long bookId;
    @Column(nullable = false)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private Date take;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private Date giveAway;
}

第二个实体BookEntity

@Entity
@Data
@NoArgsConstructor
@Table(name = "book")
public class BookEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String author;
    private Integer yearCreation;
    private Integer count;
}

和存储库

@Repository
public interface TicketRepository extends CrudRepository<TicketEntity, Long> {
    List<TicketWithBookView> findAllByGiveAwayIsNullAndTakeIsNotNull();
}

没办法)投影用于从查询中获取 select 数据,而不是从其他表中获取数据。 您可以从另一个table上传数据并在service中创建一个新的model

可能问题出在方法名称中的AAnd

findAllByGiveAwayIsNull AAndAndTakeIsNotNull

添加您收到的错误消息,这样会更容易找到问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM