Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
hibernate中对象及其所有变量的ID_Hibernate - Fatal编程技术网

hibernate中对象及其所有变量的ID

hibernate中对象及其所有变量的ID,hibernate,Hibernate,对于这个类,我希望这个类的ID是它所有属性的值。这意味着我只需要数据库中存在这些值的行。如何在Hibernate中执行此操作 public class WeatherState { private String weatherType; private double temperature; } 当持久性属性应该是WeatherState的直接属性时,就应该这样做(持久性注释是从javax.persistence包导入的): 其他选择是使用: 在这两种情况下,提供equals和

对于这个类,我希望这个类的ID是它所有属性的值。这意味着我只需要数据库中存在这些值的行。如何在Hibernate中执行此操作

public class WeatherState {
    private String weatherType;
    private double temperature;
}

当持久性属性应该是WeatherState的直接属性时,就应该这样做(持久性注释是从javax.persistence包导入的):

其他选择是使用:

在这两种情况下,提供equals和hashcode都很重要,如JPA 2.0规范中所述:

主键类必须定义equals和hashCode方法。这个 这些方法的值相等语义必须与 键所指向的数据库类型的数据库相等性 映射


你的问题很难理解,你能重新表述你的问题或提供一个你需要的例子吗?
@Entity
@IdClass(WeatherStateId.class)
public class WeatherState {
    @Id private String weatherType;
    @Id private double temperature;
    //getters, setters
}

public class WeatherStateId implements Serializable {
    private String weatherType;
    private double temperature;
    //getters, setters, equals, hashcode
}
@Entity
public class WeatherState {
    @EmbeddedId private WeatherStateId weatherStateId;

    public WeatherStateId getWeatherStateId() {
        return weatherStateId;
    }

    public void setWeatherStateId(WeatherStateId weatherStateId) {
        this.weatherStateId = weatherStateId;
    }
}

@Embeddable
public class WeatherStateId implements Serializable {
    private String weatherType;
    private double temperature;
    //getters, setters, equals, hashcode
}