系统有张表没有设置主键。只有几个字段,采用hibernate配置如下:
package com.bester.hw.domain; /** * @author xjj email: [email protected] 说明: 气象检测器报警条件表 * @date Oct 20, 2008 */ public class WeatherAlarmCondition implements java.io.Serializable { private WeatherAlarmConditionId id; public WeatherAlarmCondition() { } public WeatherAlarmCondition(WeatherAlarmConditionId id) { this.id = id; } public WeatherAlarmConditionId getId() { return this.id; } public void setId(WeatherAlarmConditionId id) { this.id = id; } }
package com.bester.hw.domain; /** * @author xjj * email: [email protected] * 说明: 气象监测器报警条件表。复合主键下的domain * @date Oct 20, 2008 */ public class WeatherAlarmConditionId implements java.io.Serializable { //基础数据 private Float visibilityMin; private Float visibilityMax; private Float roadTempMin; private Float roadTempMax; private Float roadbedTempMin; private Float roadbedTempMax; private Float sensorTempMin; private Float sensorTempMax; private Float saltTiterMin; private Float saltTiterMax; private Float freezingTempMin; private Float freezingTempMax; private Integer levigationHeightMin; private Integer levigationHeightMax; private Float rainGaugeMin; private Float rainGaugeMax; private Float windSpeedMin; private Float windSpeedMax; private Float tempMin; private Float tempMax; private Float humidityMin; private Float humidityMax; // Property accessors public Float getVisibilityMin() { return this.visibilityMin; } public void setVisibilityMin(Float visibilityMin) { this.visibilityMin = visibilityMin; } public Float getVisibilityMax() { return this.visibilityMax; } public void setVisibilityMax(Float visibilityMax) { this.visibilityMax = visibilityMax; } public Float getRoadTempMin() { return this.roadTempMin; } public void setRoadTempMin(Float roadTempMin) { this.roadTempMin = roadTempMin; } public Float getRoadTempMax() { return this.roadTempMax; } public void setRoadTempMax(Float roadTempMax) { this.roadTempMax = roadTempMax; } public Float getRoadbedTempMin() { return this.roadbedTempMin; } public void setRoadbedTempMin(Float roadbedTempMin) { this.roadbedTempMin = roadbedTempMin; } public Float getRoadbedTempMax() { return this.roadbedTempMax; } public void setRoadbedTempMax(Float roadbedTempMax) { this.roadbedTempMax = roadbedTempMax; } public Float getSensorTempMin() { return this.sensorTempMin; } public void setSensorTempMin(Float sensorTempMin) { this.sensorTempMin = sensorTempMin; } public Float getSensorTempMax() { return this.sensorTempMax; } public void setSensorTempMax(Float sensorTempMax) { this.sensorTempMax = sensorTempMax; } public Float getSaltTiterMin() { return this.saltTiterMin; } public void setSaltTiterMin(Float saltTiterMin) { this.saltTiterMin = saltTiterMin; } public Float getSaltTiterMax() { return this.saltTiterMax; } public void setSaltTiterMax(Float saltTiterMax) { this.saltTiterMax = saltTiterMax; } public Float getFreezingTempMin() { return this.freezingTempMin; } public void setFreezingTempMin(Float freezingTempMin) { this.freezingTempMin = freezingTempMin; } public Float getFreezingTempMax() { return this.freezingTempMax; } public void setFreezingTempMax(Float freezingTempMax) { this.freezingTempMax = freezingTempMax; } public Integer getLevigationHeightMin() { return this.levigationHeightMin; } public void setLevigationHeightMin(Integer levigationHeightMin) { this.levigationHeightMin = levigationHeightMin; } public Integer getLevigationHeightMax() { return this.levigationHeightMax; } public void setLevigationHeightMax(Integer levigationHeightMax) { this.levigationHeightMax = levigationHeightMax; } public Float getRainGaugeMin() { return this.rainGaugeMin; } public void setRainGaugeMin(Float rainGaugeMin) { this.rainGaugeMin = rainGaugeMin; } public Float getRainGaugeMax() { return this.rainGaugeMax; } public void setRainGaugeMax(Float rainGaugeMax) { this.rainGaugeMax = rainGaugeMax; } public Float getWindSpeedMin() { return this.windSpeedMin; } public void setWindSpeedMin(Float windSpeedMin) { this.windSpeedMin = windSpeedMin; } public Float getWindSpeedMax() { return this.windSpeedMax; } public void setWindSpeedMax(Float windSpeedMax) { this.windSpeedMax = windSpeedMax; } public Float getTempMin() { return this.tempMin; } public void setTempMin(Float tempMin) { this.tempMin = tempMin; } public Float getTempMax() { return this.tempMax; } public void setTempMax(Float tempMax) { this.tempMax = tempMax; } public Float getHumidityMin() { return this.humidityMin; } public void setHumidityMin(Float humidityMin) { this.humidityMin = humidityMin; } public Float getHumidityMax() { return this.humidityMax; } public void setHumidityMax(Float humidityMax) { this.humidityMax = humidityMax; } public boolean equals(Object other) { if ((this == other)) return true; if ((other == null)) return false; if (!(other instanceof WeatherAlarmConditionId)) return false; WeatherAlarmConditionId castOther = (WeatherAlarmConditionId) other; return ((this.getVisibilityMin() == castOther.getVisibilityMin()) || (this .getVisibilityMin() != null && castOther.getVisibilityMin() != null && this .getVisibilityMin().equals(castOther.getVisibilityMin()))) && ((this.getVisibilityMax() == castOther.getVisibilityMax()) || (this .getVisibilityMax() != null && castOther.getVisibilityMax() != null && this .getVisibilityMax() .equals(castOther.getVisibilityMax()))) && ((this.getRoadTempMin() == castOther.getRoadTempMin()) || (this .getRoadTempMin() != null && castOther.getRoadTempMin() != null && this .getRoadTempMin().equals(castOther.getRoadTempMin()))) && ((this.getRoadTempMax() == castOther.getRoadTempMax()) || (this .getRoadTempMax() != null && castOther.getRoadTempMax() != null && this .getRoadTempMax().equals(castOther.getRoadTempMax()))) && ((this.getRoadbedTempMin() == castOther.getRoadbedTempMin()) || (this .getRoadbedTempMin() != null && castOther.getRoadbedTempMin() != null && this .getRoadbedTempMin().equals( castOther.getRoadbedTempMin()))) && ((this.getRoadbedTempMax() == castOther.getRoadbedTempMax()) || (this .getRoadbedTempMax() != null && castOther.getRoadbedTempMax() != null && this .getRoadbedTempMax().equals( castOther.getRoadbedTempMax()))) && ((this.getSensorTempMin() == castOther.getSensorTempMin()) || (this .getSensorTempMin() != null && castOther.getSensorTempMin() != null && this .getSensorTempMin() .equals(castOther.getSensorTempMin()))) && ((this.getSensorTempMax() == castOther.getSensorTempMax()) || (this .getSensorTempMax() != null && castOther.getSensorTempMax() != null && this .getSensorTempMax() .equals(castOther.getSensorTempMax()))) && ((this.getSaltTiterMin() == castOther.getSaltTiterMin()) || (this .getSaltTiterMin() != null && castOther.getSaltTiterMin() != null && this .getSaltTiterMin().equals(castOther.getSaltTiterMin()))) && ((this.getSaltTiterMax() == castOther.getSaltTiterMax()) || (this .getSaltTiterMax() != null && castOther.getSaltTiterMax() != null && this .getSaltTiterMax().equals(castOther.getSaltTiterMax()))) && ((this.getFreezingTempMin() == castOther .getFreezingTempMin()) || (this.getFreezingTempMin() != null && castOther.getFreezingTempMin() != null && this .getFreezingTempMin().equals( castOther.getFreezingTempMin()))) && ((this.getFreezingTempMax() == castOther .getFreezingTempMax()) || (this.getFreezingTempMax() != null && castOther.getFreezingTempMax() != null && this .getFreezingTempMax().equals( castOther.getFreezingTempMax()))) && ((this.getLevigationHeightMin() == castOther .getLevigationHeightMin()) || (this .getLevigationHeightMin() != null && castOther.getLevigationHeightMin() != null && this .getLevigationHeightMin().equals( castOther.getLevigationHeightMin()))) && ((this.getLevigationHeightMax() == castOther .getLevigationHeightMax()) || (this .getLevigationHeightMax() != null && castOther.getLevigationHeightMax() != null && this .getLevigationHeightMax().equals( castOther.getLevigationHeightMax()))) && ((this.getRainGaugeMin() == castOther.getRainGaugeMin()) || (this .getRainGaugeMin() != null && castOther.getRainGaugeMin() != null && this .getRainGaugeMin().equals(castOther.getRainGaugeMin()))) && ((this.getRainGaugeMax() == castOther.getRainGaugeMax()) || (this .getRainGaugeMax() != null && castOther.getRainGaugeMax() != null && this .getRainGaugeMax().equals(castOther.getRainGaugeMax()))) && ((this.getWindSpeedMin() == castOther.getWindSpeedMin()) || (this .getWindSpeedMin() != null && castOther.getWindSpeedMin() != null && this .getWindSpeedMin().equals(castOther.getWindSpeedMin()))) && ((this.getWindSpeedMax() == castOther.getWindSpeedMax()) || (this .getWindSpeedMax() != null && castOther.getWindSpeedMax() != null && this .getWindSpeedMax().equals(castOther.getWindSpeedMax()))) && ((this.getTempMin() == castOther.getTempMin()) || (this .getTempMin() != null && castOther.getTempMin() != null && this.getTempMin() .equals(castOther.getTempMin()))) && ((this.getTempMax() == castOther.getTempMax()) || (this .getTempMax() != null && castOther.getTempMax() != null && this.getTempMax() .equals(castOther.getTempMax()))) && ((this.getHumidityMin() == castOther.getHumidityMin()) || (this .getHumidityMin() != null && castOther.getHumidityMin() != null && this .getHumidityMin().equals(castOther.getHumidityMin()))) && ((this.getHumidityMax() == castOther.getHumidityMax()) || (this .getHumidityMax() != null && castOther.getHumidityMax() != null && this .getHumidityMax().equals(castOther.getHumidityMax()))); } public int hashCode() { int result = 17; result = 37 * result + (getVisibilityMin() == null ? 0 : this.getVisibilityMin() .hashCode()); result = 37 * result + (getVisibilityMax() == null ? 0 : this.getVisibilityMax() .hashCode()); result = 37 * result + (getRoadTempMin() == null ? 0 : this.getRoadTempMin() .hashCode()); result = 37 * result + (getRoadTempMax() == null ? 0 : this.getRoadTempMax() .hashCode()); result = 37 * result + (getRoadbedTempMin() == null ? 0 : this.getRoadbedTempMin() .hashCode()); result = 37 * result + (getRoadbedTempMax() == null ? 0 : this.getRoadbedTempMax() .hashCode()); result = 37 * result + (getSensorTempMin() == null ? 0 : this.getSensorTempMin() .hashCode()); result = 37 * result + (getSensorTempMax() == null ? 0 : this.getSensorTempMax() .hashCode()); result = 37 * result + (getSaltTiterMin() == null ? 0 : this.getSaltTiterMin() .hashCode()); result = 37 * result + (getSaltTiterMax() == null ? 0 : this.getSaltTiterMax() .hashCode()); result = 37 * result + (getFreezingTempMin() == null ? 0 : this.getFreezingTempMin() .hashCode()); result = 37 * result + (getFreezingTempMax() == null ? 0 : this.getFreezingTempMax() .hashCode()); result = 37 * result + (getLevigationHeightMin() == null ? 0 : this .getLevigationHeightMin().hashCode()); result = 37 * result + (getLevigationHeightMax() == null ? 0 : this .getLevigationHeightMax().hashCode()); result = 37 * result + (getRainGaugeMin() == null ? 0 : this.getRainGaugeMin() .hashCode()); result = 37 * result + (getRainGaugeMax() == null ? 0 : this.getRainGaugeMax() .hashCode()); result = 37 * result + (getWindSpeedMin() == null ? 0 : this.getWindSpeedMin() .hashCode()); result = 37 * result + (getWindSpeedMax() == null ? 0 : this.getWindSpeedMax() .hashCode()); result = 37 * result + (getTempMin() == null ? 0 : this.getTempMin().hashCode()); result = 37 * result + (getTempMax() == null ? 0 : this.getTempMax().hashCode()); result = 37 * result + (getHumidityMin() == null ? 0 : this.getHumidityMin() .hashCode()); result = 37 * result + (getHumidityMax() == null ? 0 : this.getHumidityMax() .hashCode()); return result; } }
配置文件如下:
<?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.bester.hw.domain.WeatherAlarmCondition" table="T_WEATHER_ALARM_CONDITION" schema="dbo" catalog="highway"> <composite-id name="id" class="com.bester.hw.domain.WeatherAlarmConditionId"> <key-property name="visibilityMin" type="java.lang.Float"> <column name="visibility_min" precision="53" scale="0" /> </key-property> <key-property name="visibilityMax" type="java.lang.Float"> <column name="visibility_max" precision="53" scale="0" /> </key-property> <key-property name="roadTempMin" type="java.lang.Float"> <column name="road_temp_min" precision="53" scale="0" /> </key-property> <key-property name="roadTempMax" type="java.lang.Float"> <column name="road_temp_max" precision="53" scale="0" /> </key-property> <key-property name="roadbedTempMin" type="java.lang.Float"> <column name="roadbed_temp_min" precision="53" scale="0" /> </key-property> <key-property name="roadbedTempMax" type="java.lang.Float"> <column name="roadbed_temp_max" precision="53" scale="0" /> </key-property> <key-property name="sensorTempMin" type="java.lang.Float"> <column name="sensor_temp_min" precision="53" scale="0" /> </key-property> <key-property name="sensorTempMax" type="java.lang.Float"> <column name="sensor_temp_max" precision="53" scale="0" /> </key-property> <key-property name="saltTiterMin" type="java.lang.Float"> <column name="salt_titer_min" precision="53" scale="0" /> </key-property> <key-property name="saltTiterMax" type="java.lang.Float"> <column name="salt_titer_max" precision="53" scale="0" /> </key-property> <key-property name="freezingTempMin" type="java.lang.Float"> <column name="freezing_temp_min" precision="53" scale="0" /> </key-property> <key-property name="freezingTempMax" type="java.lang.Float"> <column name="freezing_temp_max" precision="53" scale="0" /> </key-property> <key-property name="levigationHeightMin" type="java.lang.Integer"> <column name="levigation_height_min" /> </key-property> <key-property name="levigationHeightMax" type="java.lang.Integer"> <column name="levigation_height_max" /> </key-property> <key-property name="rainGaugeMin" type="java.lang.Float"> <column name="rain_gauge_min" precision="53" scale="0" /> </key-property> <key-property name="rainGaugeMax" type="java.lang.Float"> <column name="rain_gauge_max" precision="53" scale="0" /> </key-property> <key-property name="windSpeedMin" type="java.lang.Float"> <column name="wind_speed_min" precision="53" scale="0" /> </key-property> <key-property name="windSpeedMax" type="java.lang.Float"> <column name="wind_speed_max" precision="53" scale="0" /> </key-property> <key-property name="tempMin" type="java.lang.Float"> <column name="temp_min" precision="53" scale="0" /> </key-property> <key-property name="tempMax" type="java.lang.Float"> <column name="temp_max" precision="53" scale="0" /> </key-property> <key-property name="humidityMin" type="java.lang.Float"> <column name="humidity_min" precision="53" scale="0" /> </key-property> <key-property name="humidityMax" type="java.lang.Float"> <column name="humidity_max" precision="53" scale="0" /> </key-property> </composite-id> </class> </hibernate-mapping>
然后通过hibernate进行查找没有问题,可是就是无法进行更新。结合Struts2使用,代码如下:
/** * 功能:转向修改视图 * @return * @date Oct 23, 2008 * @time 3:42:44 PM */ public String toUpdateAC(){ List<WeatherAlarmCondition> wacList=cs.getWeatherAlarmConditionService().findAll(new WeatherAlarmCondition()); if(wacList.size()!=0){ ac=wacList.get(0); }; this.initLog(); return SUCCESS; } /** * 功能:修改气象检测器报警条件 * @return * @date Oct 23, 2008 * @time 3:49:21 PM */ public String updateAC(){ cs.getWeatherAlarmConditionService().update(ac); this.operateString="修改气象检测器报警条件"; return SUCCESS; }
如果使用saveOrUpdate,merge方法,则更改都变成了重新插入。如果用update方法,数据库数据无任何变化。难道没有主键的情况下,hibernate的session中不对对象的状态进行自动维护吗?