quotation_history_data表trade_date    code    abbreviation
20090326      H11030  SAC农林
20090327      H11030  SAC农林
20090326      H11031  SAC农林
20090327      H11031  SAC农林将”H11030“的“20090327”和”H11031“的“20090327”取出来。sql server2000 sql语句 
SELECT max(trade_date),code,abbreviation
FROM quotation_history_data
GROUP BY code,abbreviation HibernateDAO.java
package com.geong.project.dao;import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import com.geong.project.Logger;
import com.geong.sac.cx.QuotationHistoryDataDemonstration;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.type.Type;
/**
 * @author lianhuigang
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class HibernateDAO implements Logger
{
private static final SessionFactory sessionFactory; static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage());
}
} public static final ThreadLocal threadSession = new ThreadLocal(); public static Session currentSession() throws HibernateException {
Session s = (Session) threadSession.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
threadSession.set(s);
}
return s;
}

public static void closeSession()  
{
try
{
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null)
s.close();
}
catch (HibernateException e)
{
logger.error("Error when closing session:", e);
}
}public static Vector loadObjects(String query) throws DbException
{
Transaction tx = null;

try
{
Session session = currentSession();
tx = (Transaction)session.beginTransaction(); Vector result = loadObjects(session, query); tx.commit();

return result; }
catch (Exception e)
{
if (tx != null)
try
{
tx.rollback();
}
catch (Exception e1)
{
}
logger.error("query object error, query:" + query, e);
throw new DbException(e);
}
finally
{
closeSession();
}
}private static Vector loadObjects(Session session, String query) throws DbException
{
Vector retVec = new Vector();
try
{
List objs = session.find(query);
retVec = new Vector(objs);

}
catch (HibernateException e)
{
throw new DbException(e);
}

return retVec;
}
}select.jsp<%@ page language="java" contentType="text/html;charset=GBK"%><%@ page import="java.util.Vector"%>
<%@ page import="java.util.*"%>
<%@ page import="com.geong.sac.cx.QuotationHistoryDataDemonstration"%>
<%@ page import="com.geong.project.dao.HibernateDAO"%>
<html>
<body> <TABLE border="1">
  <TR style="background-color: rgb(0, 255, 64);">
   <TD><font color="#ff0080"><b>交易日期_代码</b></font></TD>
<TD><font color="#ff0080"><b>交易日期</b></font></TD>
<TD><font color="#ff0080"><b>代码</b></font></TD>
<TD><font color="#ff0080"><b>简称</b></font></TD>
<TD><font color="#ff0080"><b>开盘</b></font></TD>
<TD><font color="#ff0080"><b>最高</b></font></TD>
<TD><font color="#ff0080"><b>最低</b></font></TD>
<TD><font color="#ff0080"><b>收盘</b></font></TD>
<TD><font color="#ff0080"><b>涨跌</b></font></TD>
<TD><font color="#ff0080"><b>涨跌率</b></font></TD>
<TD><font color="#ff0080"><b>成交量</b></font></TD>
<TD><font color="#ff0080"><b>成交金额</b></font></TD>
<TD><font color="#ff0080"><b>市盈率1</b></font></TD>
<TD><font color="#ff0080"><b>市盈率2</b></font></TD>
<TD><font color="#ff0080"><b>股息率1</b></font></TD>
<TD><font color="#ff0080"><b>股息率2</b></font></TD>
  </TR>
  
  <%  
 Vector vec = HibernateDAO.loadObjects("SELECT max(quota.trade_date),quota.abbreviation from QuotationHistoryDataDemonstration as quota group by quota.code,quota.abbreviation "); for(int i=0;i<vec.size();i++){ 
 
QuotationHistoryDataDemonstration  qu = (QuotationHistoryDataDemonstration)vec.get(i); 
%>  <TR >
  
<TD><font color="#ff0080"><b><%=qu.getTrade_date() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getCode() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getAbbreviation() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getOpening_price() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getMaximum() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getMinimum() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getClosing_price() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getRising_falling() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getRising_falling_percentage() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getTurnover() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getDeal_amount() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getMarket_profit1() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getMarket_profit2() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getRate_of_dividend1() %></b></font></TD>
<TD><font color="#ff0080"><b><%=qu.getRate_of_dividend2() %></b></font></TD>

  </TR>
  
  
<%}%>
  
  
  </TABLE></body>
</html>
数据怎么老是查不出来啊。是不是“SELECT max(quota.trade_date),quota.abbreviation from QuotationHistoryDataDemonstration as quota group by quota.code,quota.abbreviation”hql语句有问题啊我的hibernate是2.1版本的

解决方案 »

  1.   

    hibernate执行SQL语句:
    List cats = sess.createSQLQuery("select {cat.*} from cats cat")
        .addEntity("cat", Cat.class)
        .list();这个查询指定了: SQL查询语句,它带一个占位符,可以让Hibernate使用字段的别名. 查询返回的实体,和它的SQL表的别名. addEntity()方法将SQL表的别名和实体类联系起来,并且确定查询结果集的形态。 addJoin()方法可以被用于载入其他的实体和集合的关联. List cats = sess.createSQLQuery(
            "select {cat.*}, {kitten.*} from cats cat, cats kitten where kitten.mother = cat.id"
        )
        .addEntity("cat", Cat.class)
        .addJoin("kitten", "cat.kittens")
        .list();原生的SQL查询可能返回一个简单的标量值或者一个标量和实体的结合体。 Double max = (Double) sess.createSQLQuery("select max(cat.weight) as maxWeight from cats cat")
            .addScalar("maxWeight", Hibernate.DOUBLE);
            .uniqueResult();
      

  2.   

    老是报强制转化问题啊:java.lang.ClassCastException
    这行老是出错啊: QuotationHistoryDataDemonstration  qu = (QuotationHistoryDataDemonstration)vec.get(i);