I'm quite new for Hibernate. I have table A, B and C as following below
As condition: C have foreignKey of B and B have foreignKey of A.
I use ScrollableResults class to get total Records for performance improvements.
Code:
ScrollableResults resultScroll = query.setCacheMode(CacheMode.IGNORE).scroll();
resultScroll.last(); // get last records
int totalRecords = resultScroll.getRowNumber() + 1;
if (resultScroll != null)
resultScroll.close();
If i use first hql, scrollableResults show correct total records, my data is 31 records as show below Code:
Select at from A at inner join fetch at.bf
If i use second hql, scrollableResults show wrong total records, it is 38 records as shown below. Code:
Select at from
A at inner join fetch at.bf bt
inner join fetch bt.cf
If i use third hql, scrollableResults show wrong total records, it is 31 records as shown below. Code:
Select at from
A at inner join fetch at.bf bt
inner join bt.cf
If i use
getResultList(), 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.Table ACode:
@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;
}
Table BCode:
@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;
}
Table CCode:
@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;
}
Best Regards