java ScrollableResult显示错误的totalRecords以与hql结果和getResultList()进行比较
我对Hibernate很陌生,这是第一次在StackOverFlow的论坛上发表文章。我有如下表A、B和C
条件:C的外键为B,B的外键为A
表A
@Entity
@Table(name = "A")
public class A implements Serializable {
private int aId;
private String aName;
private List<B> bf= new ArrayList<B>();
public A() {
}
public A(int aId, String aName) {
this.aId= aId;
this.aName= aName;
}
@Id
@Column(name = "A_ID", unique = true, nullable = false)
public int getAId() {
return this.aId;
}
public void setAId(int aId) {
this.aId= aId;
}
@Column(name = "A_NAME", nullable = false)
public String getAName() {
return this.aName;
}
public void setAName(String aName) {
this.aName = aName;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "bf")
public Set<B> getBF() {
return this.bf;
}
public void setBF(Set<B> bf) {
this.bf = bf;
}
}
表B
@Entity
@Table(name = "B")
public class B implements Serializable {
private int bId;
private String bName;
private A af;
private List<C> cf= new ArrayList<C>();
public B() {
}
public B(int bId, String bName) {
this.bId= bId;
this.bName= bName;
}
@Id
@Column(name = "B_ID", unique = true, nullable = false)
public int getBId() {
return this.bId;
}
public void setBId(int bId) {
this.bId= bId;
}
@Column(name = "B_NAME", nullable = false)
public String getBName() {
return this.bName;
}
public void setBName(String bName) {
this.bName = bName;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "A_ID")
public A getAF() {
return this.af;
}
public void setAF(A af) {
this.af= af;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "cf")
public Set<C> getCF() {
return this.cf;
}
public void setCF(Set<C> cf) {
this.cf= cf;
}
}
表C
@Entity
@Table(name = "C")
public class C implements Serializable {
private int cId;
private String cName;
private B bf;
public C() {
}
public C(int cId, String cName) {
this.cId= cId;
this.cName= cName;
}
@Id
@Column(name = "C_ID", unique = true, nullable = false)
public int getCId() {
return this.cId;
}
public void setCId(int cId) {
this.cId= cId;
}
@Column(name = "C_NAME", nullable = false)
public String getCName() {
return this.cName;
}
public void setCName(String cName) {
this.cName = cName;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "B_ID")
public B getBF() {
return this.bf;
}
public void setBF(B bf) {
this.bf= bf;
}
}
我使用ScrollableResults类获取性能改进的总记录
ScrollableResults resultScroll = query.setCacheMode(CacheMode.IGNORE).scroll();
resultScroll.last(); // get last records
int totalRecords = resultScroll.getRowNumber() + 1;
if (resultScroll != null)
resultScroll.close();
如果我使用第一个hql,可滚动结果将显示31条记录,如下所示
Select at from A at inner join fetch at.bf
如果我使用第二个hql,滚动结果显示38条记录,如下所示
Select at from
A at inner join fetch at.bf bt
inner join fetch bt.cf
如果我使用第三个hql,scrollableResults会显示正确的总记录,它是31条记录,如下所示
Select at from
A at inner join fetch at.bf bt
inner join bt.cf
If i use getResultList method, it always get 31 records. I think that scroll() has something wrong with two-level fetch but it work both of one-level fetch and without fetch.
有关更多信息:
-Hibernate 5.2.2-Final
-微软SQL Server 2012
-JDBC驱动程序:sqljdbc4 3.0版
谢谢!
共 (0) 个答案