First of all, this is the first time that I use Hibernate so my questions might seem naive.
I have two tables, DsJobs and DsEvents. Events has a foreign key relationship on the DsJobs table (EID column in DsJobs table corresponds to the ID column of DsEvents table).
I have creted the mapping with anotations, what I care about is unilateral relation, so I have a 'dsEvents' property in DsJobs table annotated with @ManyToOne:
Code:
@Entity
@Table(name="DsJobs")
public class DsJobs implements java.io.Serializable {
...
private DsEvents dsEvents;
...
@Id
@Column(name="JID", unique=true, nullable=false)
public long getJid() {
return this.jid;
}
public void setJid(long jid) {
this.jid = jid;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="eid")
public DsEvents getDsEvents() {
return this.dsEvents;
}
public void setDsEvents(DsEvents dsEvents) {
this.dsEvents = dsEvents;
}
....
}
I want to retrieve the list of Jobs records from the databse and restricte them with setFirstResult and setMaxResults. This is the call i make:
Code:
return (ArrayList<DsJobs>) this.sessionFactory.getCurrentSession().createQuery("from DsJobs log").setFirstResult(start).setMaxResults(rows).list();
This code returns an ArrayList of DsJobs object and inside every DsJobs object there is a DsEvents object, correctly instantiated.
My issues are two:
1) When I see the HQL executed there are unnecesery SELECT calls to the database, one for every DsJobs record. So for each DsJobs record, there is a SELECT in the DsEvents to get the coresponding event, by event id. This is unacceptable for performance, since with simple JDBC I can get all the data I need with a inner join SELECT stament. I am probably doing something completely wrong, please let me know what and if you know how I should proceed.
2) The setFirstResult and setMaxResults seem to be completeley ignored by Hibernate, I always get all records from the database. How can I fix that?
I will greatly appreciate any help.
Thank you in advance.