`
exceljava
  • 浏览: 160282 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

hibernate无主键情况下无法更新

    博客分类:
  • java
阅读更多

系统有张表没有设置主键。只有几个字段,采用hibernate配置如下:

package com.bester.hw.domain;

/**
 * @author xjj email: exceljava@163.com 说明: 气象检测器报警条件表
 * @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: exceljava@163.com
* 说明:  气象监测器报警条件表。复合主键下的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中不对对象的状态进行自动维护吗?

分享到:
评论

相关推荐

    hibernate 无主键表映射

    在Java的持久化框架Hibernate中,无主键表映射是一种特殊情况,主要处理那些在数据库中没有明确单一主键的表。这种情况通常出现在那些通过多个字段共同唯一标识一条记录的复合主键(Composite Key)场景。本文将详细...

    hibernate联合主键全攻略

    在探讨Hibernate中联合主键的使用时,我们首先需要理解什么是联合主键以及它在Hibernate框架中的实现方式。联合主键(Composite Primary Key),是指在数据库表中使用两个或多个字段共同作为主键来唯一标识每一行...

    Hibernate各种主键生成策略与配置详解

    关于Hibernate的各种主键生成策略与配置详解

    Hibernate复合主键

    "Hibernate复合主键" Hibernate复合主键是指在 Hibernate 框架中使用复合主键来唯一标识一个实体。复合主键是指由多个字段组成的主键,用于唯一标识一个实体。在本例中,我们将通过一个简单的复合主键的做关联类的...

    hibernate复合主键配置和使用

    在数据库设计中,复合主键是解决某些特定情况下的有效手段。 二、Hibernate中的复合主键配置 在Hibernate中,我们可以通过`@Embeddable`和`@EmbeddedId`注解来实现复合主键的配置。 1. 创建复合主键类 首先,...

    hibernate复合主键设置

    在Java的持久化框架Hibernate中,复合主键(Composite Key)是一种特殊的数据结构,用于处理具有多个字段作为唯一标识的情况。本教程将深入探讨如何在Hibernate中设置复合主键,并提供一个可直接使用的配置示例。 ...

    Hibernate中主键生成策略

    在Java的持久化框架Hibernate中,主键生成策略是一个至关重要的概念,它决定了数据库表中主键值如何自动生成。主键通常是表中唯一标识记录的一列,对于数据的完整性和一致性至关重要。以下是对Hibernate中主键生成...

    Hibernate主键生成方式

    以下是关于Hibernate主键生成方式的详细说明: 1. **Identity方式**:`&lt;generator class="identity"/&gt;` 这种方式依赖于数据库自身的自动增长机制。例如,在MySQL中,它可以与`AUTO_INCREMENT`属性配合使用。当...

    hibernate使用主键关联的一对一关系

    在这个场景中,“hibernate使用主键关联的一对一关系”指的是通过共享主键来实现两个实体类之间的这种一对一绑定。下面我们将深入探讨这一主题。 首先,我们需要理解一对一关系的概念。在数据库中,一对一关系意味...

    hibernate映射主键生成策略native

    本文将重点介绍在Oracle数据库环境下,使用Hibernate的“native”策略来生成主键的具体方法及注意事项。 #### 二、主键生成策略概述 在Hibernate中,主键生成策略主要包括以下几种: - **identity**:由数据库...

    Hibernate联合主键的例子

    这种设计模式常用于那些自然主键不适合的情况,例如,当单一字段无法唯一标识一条记录时。在Hibernate中,我们可以使用`@Embeddable`和`@EmbeddedId`注解来定义和使用联合主键。 1. **创建联合主键类:** 在...

    hibernate复合主键映射

    复合主键映射 &lt;br&gt;通常将复合主键相关属性,单独抽取出来,建立一个独立的类 * 必须实现序列化接口 * 必须实现equals和hashcode方法 采用标签进行映射,其它属性采用正常映射

    Hibernate各种主键生成策略

    Hibernate各种主键生成策略详解,包括 assigned increment hilo seqhilo sequence identity native uuid foreign uuid.hex sequence-identity 等

    Hibernate主键生成策略

    ### Hibernate 主键生成策略详解 #### 一、概述 Hibernate 是一款开源的对象关系映射 (ORM) 框架,它允许开发人员将 Java 对象映射到数据库表中的记录,从而极大地简化了数据访问层的开发工作。在 Hibernate 中,...

    hibernate主键生成策略详解

    ### hibernate主键生成策略详解 #### 一、assigned **assigned** 主键生成策略意味着主键的值是由外部程序负责生成的,并且在执行 `save()` 方法之前必须明确指定一个值。在这种策略下,Hibernate 不参与主键的...

    hibernate主键生成策略

    ### Hibernate 主键生成策略详解 #### 一、概述 Hibernate 是一款开源的对象关系映射 (ORM) 框架,它极大地简化了 Java 应用程序与数据库之间的交互过程。在使用 Hibernate 进行持久化操作时,经常需要处理实体类...

    hibernate复合主键的实例

    在某些情况下,单一字段无法唯一标识表中的记录,此时需要多个字段联合起来作为主键。例如,考虑一个学生选课表,可能包含"学生ID"和"课程ID",这两者结合才能唯一确定学生选修的特定课程。 三、Hibernate中复合...

    hibernate的联合主键怎么设置

    在 Hibernate 中,联合主键(Composite Key)是指由两个或多个属性共同构成的主键,这种主键在数据库设计中常见于需要多个字段唯一标识一条记录的情况。在 Hibernate 中设置联合主键通常需要以下几个步骤: 1. **...

Global site tag (gtag.js) - Google Analytics