JPA存储库连接返回所有记录

时间:2015-01-12 20:33:15

标签: 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 = "paper", fetch = FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
private List<Stock> stocks;
...

@Entity(name="STOCK")
public class Stock implements Serializable {

@Id
@GeneratedValue
@Column(name="stock_id")
private Long id;
@Column
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime date;
@Column(name="opening_Price", columnDefinition="Decimal(10,2) default '0.00'")
private Double openingPrice;
@Column(name="top_price", columnDefinition="Decimal(10,2) default '0.00'")
private Double topPrice;
@Column(name="lowest_price", columnDefinition="Decimal(10,2) default '0.00'")
private Double lowestPrice;
@Column(name="closing_price", columnDefinition="Decimal(10,2) default '0.00'")
private Double closingPrice;
private Long volume;
@Lob @Basic(fetch = FetchType.LAZY)
@Column(length=100000)
private byte [] graph;

@ManyToOne
@JoinColumn(name = "paper_id")
private Paper paper;

非常多的一对多关系。我有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(String date):

private DateTime resolveDate(String date) {
    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    DateTime dateTime = formatter.parseDateTime(date+" "+"00:00:00");
    return dateTime;
}

我想查询等于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

0 个答案:

没有答案