Hi there, newbie here.
I'm doing a project where we have, among others, 2 tables in a database (DB2 8.1 using legacy CLI JDBC driver): "Paper" and "Quote". As you might've guessed, the paper-table contains information about stock papers, whereas the quote-table contains streaming quotes for the papers.
The two tables are related to each other via paper.id and quote.fk_paper.
I need to be able to get the newest row from the quote table, for all or some papers, in one go (=one HQL). The result should then be a List of Quote objects - one quote for each paper.
What I do now is this:
Code:
/** Get the Quotes for all papers in the database */
public List getQuotes(Long paperTypeId) throws DAOFinderException {
List list = new ArrayList();
log.debug("Getting list of Quotes for Papers of type " + paperTypeId);
try {
List ps = new ArrayList();
if (paperTypeId==null)
ps = (List) getQuotes();
else
ps = (List) ses.createQuery("select distinct p.id from Paper p where p.type.id=:id")
.setLong("id", paperTypeId.longValue())
.list();
// TODO: There MUST be a better way to do this, mann!
Iterator itr = ps.iterator();
while (itr.hasNext()){
list.add(getQuote((Long)itr.next()));
}
}
catch (HibernateException e) {
// TODO: What about the Session!?!?!
throw new DAOFinderException(""+e);
}
return list;
}
/** Get the Quotes that belongs to the supplied Paper ID*/
public Quote getQuote(Long paperId) throws DAOFinderException {
Quote q = null;
try{
q = (Quote) ses.createQuery("from Quote q where q.papir.id=:id order by q.id desc")
.setLong("id", paperId.longValue())
.setMaxResults(1)
.uniqueResult();
}
catch(HibernateException e){
throw new DAOFinderException("Couldn't get single Quote for Paper w/id=" + paperId + ": " + e);
}
return q;
}
The mapping files are as follows:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="dk.xyz.web.model.Quote" table="quote" schema="wasadmin" dynamic-insert="true">
<!-- INFO -->
<id column="id" name="id">
<generator class="identity" />
</id>
<!--discriminator column="fk_paper" type="long" /-->
<property column="tid" name="tid" not-null="true" />
<many-to-one name="papir" column="fk_paper" not-null="true" />
<property column="bud" name="bud" not-null="true" />
<property column="udbud" name="udbud" not-null="true" />
<property column="seneste" name="seneste" not-null="true" />
<property column="senestegain" name="senesteGain" not-null="true" />
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="dk.xyz.web.model.Paper" table="paper" schema="wasadmin">
<!-- INFO -->
<id column="id" name="id" unsaved-value="null">
<generator class="identity" />
</id>
<property column="name" name="name" not-null="true" />
<property column="kode" name="code" />
<property column="isin" name="isin" />
</class>
</hibernate-mapping>
Please help anyone, I'm on a very tight schedule here!?
Thanks in advance!
/Henrik