hibernate 多对一联表查询
多
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 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>
/** * 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 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>
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);
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的对象列表