Java JPA存储库联接返回所有记录
我现在面临的问题是,jpa查询返回所有记录,而不仅仅是给定范围内的记录。我肯定我错过了什么,但我真的不知道是什么。jpa连接是否正确编写?实体:Java JPA存储库联接返回所有记录,java,mysql,hibernate,jpa,spring-data-jpa,Java,Mysql,Hibernate,Jpa,Spring Data Jpa,我现在面临的问题是,jpa查询返回所有记录,而不仅仅是给定范围内的记录。我肯定我错过了什么,但我真的不知道是什么。jpa连接是否正确编写?实体: @Entity public class Paper implements Serializable{ @Id @GeneratedValue @Column(name = "paper_id") private Long paperId; private String paperName; @OneToMany(mappedBy = "pape
@Entity
public class Paper implements Serializable{
@Id
@GeneratedValue
@Column(name = "paper_id")
private Long paperId;
private String paperName;
@OneToMany(mappedBy = "paper", fetch = FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
private List<Stock> stocks;
...
几乎是一对多的关系。我有一个jpa存储库,其中包括:
@Query("select p from Paper p inner join p.stocks s where (s.date between :preDateTime and :postDateTime) and p.paperName=:paperName")
Paper findByPaperNameAndStockDate(@Param("paperName") String paperName, @Param("preDateTime") DateTime preDateTime, @Param("postDateTime") DateTime postDateTime);
我这样称呼这个方法:
DateTime date = resolveDate(dateTime);
DateTime preDate=date.minusDays(1);
DateTime postDate=date.plusDays(1);
return paperConverter.convertToDto(paperRepository.findByPaperNameAndStockDate(paperName,preDate,postDate));
resolveDate(字符串日期):
我想要一个等于mysql的查询:
select * from Paper inner join STOCK on STOCK.paper_id=Paper.paper_id where Paper.paperName='xyz' and STOCK.date between '2014-12-28 00:00:00' and '2014-12-30 00:00:00' ;
运行此方法后,我可以看到两个查询:
Hibernate: select paper0_.paper_id as paper1_0_, paper0_.paperName as paperName0_ from Paper paper0_ inner join STOCK stocks1_ on paper0_.paper_id=stocks1_.paper_id where (stocks1_.date between ? and ?) and paper0_.paperName=? limit ?
第二个:
Hibernate: select stocks0_.paper_id as paper9_0_1_, stocks0_.stock_id as stock1_1_, stocks0_.stock_id as stock1_2_0_, stocks0_.closing_price as closing2_2_0_, stocks0_.date as date2_0_, stocks0_.graph as graph2_0_, stocks0_.lowest_price as lowest5_2_0_, stocks0_.opening_Price as opening6_2_0_, stocks0_.paper_id as paper9_2_0_, stocks0_.top_price as top7_2_0_, stocks0_.volume as volume2_0_ from STOCK stocks0_ where stocks0_.paper_id=?
日志行:
12:17:43,320 DEBUG SQL:104 - select paper0_.paper_id as paper1_0_, paper0_.paperName as paperName0_ from Paper paper0_ inner join STOCK stocks1_ on paper0_.paper_id=stocks1_.paper_id where (stocks1_.date between ? and ?) and paper0_.paperName=? limit ?
Hibernate: select paper0_.paper_id as paper1_0_, paper0_.paperName as paperName0_ from Paper paper0_ inner join STOCK stocks1_ on paper0_.paper_id=stocks1_.paper_id where (stocks1_.date between ? and ?) and paper0_.paperName=? limit ?
12:17:43,405 TRACE BasicBinder:83 - binding parameter [1] as [TIMESTAMP] - 2014-12-27 23:00:00.0
12:17:43,437 TRACE BasicBinder:83 - binding parameter [2] as [TIMESTAMP] - 2014-12-29 23:00:00.0
12:17:43,441 TRACE BasicBinder:83 - binding parameter [3] as [VARCHAR] - xyz
你能打开hibernate属性中的SQL并查看什么SQL被发送到db吗?我已经用查询更新了帖子。我为我之前忘记做这件事道歉。查看完整的日志记录会很有趣,如果您设置loglevel=trace,您将看到参数值和每个查询调用。我已经附加了日志行,奇怪的是这些日期,如果要列出的日期是2014-12-29,我预计会是2014-12-30和2014-12-28
Hibernate: select stocks0_.paper_id as paper9_0_1_, stocks0_.stock_id as stock1_1_, stocks0_.stock_id as stock1_2_0_, stocks0_.closing_price as closing2_2_0_, stocks0_.date as date2_0_, stocks0_.graph as graph2_0_, stocks0_.lowest_price as lowest5_2_0_, stocks0_.opening_Price as opening6_2_0_, stocks0_.paper_id as paper9_2_0_, stocks0_.top_price as top7_2_0_, stocks0_.volume as volume2_0_ from STOCK stocks0_ where stocks0_.paper_id=?
12:17:43,320 DEBUG SQL:104 - select paper0_.paper_id as paper1_0_, paper0_.paperName as paperName0_ from Paper paper0_ inner join STOCK stocks1_ on paper0_.paper_id=stocks1_.paper_id where (stocks1_.date between ? and ?) and paper0_.paperName=? limit ?
Hibernate: select paper0_.paper_id as paper1_0_, paper0_.paperName as paperName0_ from Paper paper0_ inner join STOCK stocks1_ on paper0_.paper_id=stocks1_.paper_id where (stocks1_.date between ? and ?) and paper0_.paperName=? limit ?
12:17:43,405 TRACE BasicBinder:83 - binding parameter [1] as [TIMESTAMP] - 2014-12-27 23:00:00.0
12:17:43,437 TRACE BasicBinder:83 - binding parameter [2] as [TIMESTAMP] - 2014-12-29 23:00:00.0
12:17:43,441 TRACE BasicBinder:83 - binding parameter [3] as [VARCHAR] - xyz