首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

hibernate 多对一联表查询,该怎么处理

2012-01-24 
hibernate 多对一联表查询多Java codeimport java.util.Date/** * Weather entity. @author MyEclipse Pe

hibernate 多对一联表查询

Java code
import java.util.Date;/** * Weather entity. @author MyEclipse Persistence Tools */public class Weather implements java.io.Serializable {    // Fields    private String guid;    [color=#FF0000]private Station station;[/color]    private Date date;    private Date spiderDate;    private Short week;    private String bwea;    private String ewea;    private Short tmax;    private Short tmin;    private String bwind;    private String ewind;    private String bwindPower;    private String ewindPower;    // Constructors    /** default constructor */    public Weather() {    }    /** full constructor */    public Weather(Station station, Date date, Date spiderDate, Short week,            String bwea, String ewea, Short tmax, Short tmin, String bwind,            String ewind, String bwindPower, String ewindPower) {        this.station = station;        this.date = date;        this.spiderDate = spiderDate;        this.week = week;        this.bwea = bwea;        this.ewea = ewea;        this.tmax = tmax;        this.tmin = tmin;        this.bwind = bwind;        this.ewind = ewind;        this.bwindPower = bwindPower;        this.ewindPower = ewindPower;    }    get(),set()...............}

XML code
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping>    <class name="com.scyb.iptv.weather.common.bo.Weather" table="weather" catalog="weather_info">        <id name="guid" type="java.lang.String">            <column name="GUID" length="32" />            <generator class="uuid.hex" />        </id>        [color=#FF0000]<many-to-one name="station" class="com.scyb.iptv.weather.common.bo.Station">            <column name="STATION_NO" length="32" unique="true">                <comment>站点号</comment>            </column>        </many-to-one>[/color]        <property name="date" type="java.util.Date">            <column name="DATE" length="10">                <comment>天气日期</comment>            </column>        </property>        <property name="spiderDate" type="java.util.Date">            <column name="SPIDER_DATE" length="10">                <comment>爬虫日期</comment>            </column>        </property>        <property name="week" type="java.lang.Short">            <column name="WEEK">                <comment>星期</comment>            </column>        </property>        <property name="bwea" type="java.lang.String">            <column name="BWEA" length="6">                <comment>白天天气</comment>            </column>        </property>        <property name="ewea" type="java.lang.String">            <column name="EWEA" length="6">                <comment>夜间天气</comment>            </column>        </property>        <property name="tmax" type="java.lang.Short">            <column name="TMAX">                <comment>最高温度</comment>            </column>        </property>        <property name="tmin" type="java.lang.Short">            <column name="TMIN">                <comment>最低温度</comment>            </column>        </property>        <property name="bwind" type="java.lang.String">            <column name="BWIND" length="6">                <comment>白天风向</comment>            </column>        </property>        <property name="ewind" type="java.lang.String">            <column name="EWIND" length="6">                <comment>夜间风向</comment>            </column>        </property>        <property name="bwindPower" type="java.lang.String">            <column name="BWIND_POWER" length="8">                <comment>白天风力</comment>            </column>        </property>        <property name="ewindPower" type="java.lang.String">            <column name="EWIND_POWER" length="8">                <comment>夜间风力</comment>            </column>        </property>    </class></hibernate-mapping> 



Java code
/** * Station entity. @author MyEclipse Persistence Tools */public class Station implements java.io.Serializable {    // Fields    private Integer stationNo;    private String guid;    private String continent;    private String country;    private String province;    private String cityName;    private String counties;    private String url;    [color=#FF0000]private Set weathers = new HashSet(0);[/color]    // Constructors    /** default constructor */    public Station() {    }    /** full constructor */    public Station(String guid, String continent, String country,            String province, String cityName, String counties, String url,            Set weathers, Set exps) {        this.guid = guid;        this.continent = continent;        this.country = country;        this.province = province;        this.cityName = cityName;        this.counties = counties;        this.url = url;        this.weathers = weathers;        this.exps = exps;    }    // Property accessors    get(),set()...............}

XML code
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping>    <class name="com.scyb.iptv.weather.common.bo.Station" table="station" catalog="weather_info">        <id name="stationNo" type="java.lang.Integer">            <column name="STATION_NO" />            <generator class="uuid.hex" />        </id>        <property name="guid" type="java.lang.String">            <column name="GUID" length="32">                <comment>主键</comment>            </column>        </property>        <property name="continent" type="java.lang.String">            <column name="CONTINENT" length="8">                <comment>洲</comment>            </column>        </property>        <property name="country" type="java.lang.String">            <column name="COUNTRY" length="8">                <comment>国家</comment>            </column>        </property>        <property name="province" type="java.lang.String">            <column name="PROVINCE" length="8">                <comment>省</comment>            </column>        </property>        <property name="cityName" type="java.lang.String">            <column name="CITY_NAME" length="8">                <comment>市</comment>            </column>        </property>        <property name="counties" type="java.lang.String">            <column name="COUNTIES" length="8">                <comment>区县</comment>            </column>        </property>        <property name="url" type="java.lang.String">            <column name="URL" length="200">                <comment>天气网地址</comment>            </column>        </property>        <set name="weathers" inverse="true" lazy="true">            <key>                <column name="STATION_NO" unique="true">                    <comment>站点号</comment>                </column>            </key>            <one-to-many class="com.scyb.iptv.weather.common.bo.Weather" />        </set>    </class></hibernate-mapping> 



HQL
Java code
String hql = "from Station as sta left join sta.weathers wea where wea.spiderDate = curdate() and wea.date = curdate() and sta.stationNo like '10101%'";        List list = this.queryList(hql);

list.size()为什么是0,生成的SQL放在数据库里查询却有记录。
当把hql的where条件语句删掉后,再执行list.size()就能查到东西了。
需求:从Station,Weather中取几个属性的值,拼接在一起。条件就是spiderDate,date要当日的,stationNo要10101%的。
跪求正确HQL写法。


[解决办法]
我觉得,楼主就不应该用join这种东西,这种东西就不应该在HQL中出现,一出现他就不应该是HQL而应该是SQL

Java code
String hql = "from Weather as wea where model wea.spiderDate = curdate() and wea.date = curdate()  and wea.station.stationNo like '10101%'";List list = this.queryList(hql);//返回的list是一个Weather的对象列表 

热点排行