首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据设计正确地将这个关系编码成多个关系

如何根据设计正确地将这个关系编码成多个关系
EN

Stack Overflow用户
提问于 2021-08-13 02:47:26
回答 1查看 168关注 0票数 0

我很难用H2数据库在Java中编写以下设计,我想我成功地完成了WeatherData表与位置的关系,但是我无法正确地使用WeatherData/WeatherDataTemperature关系(一对多),这是因为在WeatherDataTemperature Hibernate类中,必须添加主键,但是正如您在我的模型中所看到的,这只是一个练习,但是我被阻止了,看到了Baeldung教程(https://www.baeldung.com/hibernate-one-to-many),但是仍然被困住了,这是我所得到的,但现在我不能将许多温度分配给WeatherData,因为@Id是一个主键,当然不能重复,请帮助

我有以下课程:

WeatherData

代码语言:javascript
复制
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity
public class WeatherData {

    @Id
    @GeneratedValue
    Long id;
    LocalDate date;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "location_id", referencedColumnName = "id")
    Location location;
    // Here is the problem im having I think
    @OneToMany(mappedBy = "weatherData")
    private java.util.Set<WeatherDataTemperature> weatherDataTemperature;

    WeatherData() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public LocalDate getDate() {
        return date;
    }

    public void setDate(LocalDate date) {
        this.date = date;
    }

    public Location getLocationId() {
        return location;
    }

    public void setLocationId(Location location) {
        this.location = location;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public java.util.Set<WeatherDataTemperature> getWeatherDataTemperature() {
        return weatherDataTemperature;
    }

    public void setWeatherDataTemperature(java.util.Set<WeatherDataTemperature> weatherDataTemperature) {
        this.weatherDataTemperature = weatherDataTemperature;
    }

}

Location

代码语言:javascript
复制
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Location {

    @Id
    @GeneratedValue
    Long id;
    Double lat;
    Double lon;
    String city;
    String state;

    @OneToOne(mappedBy = "location")
    private WeatherData weatherData;

    Location() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Double getLat() {
        return lat;
    }

    public void setLat(Double lat) {
        this.lat = lat;
    }

    public Double getLon() {
        return lon;
    }

    public void setLon(Double lon) {
        this.lon = lon;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public WeatherData getWeatherData() {
        return weatherData;
    }

    public void setWeatherData(WeatherData weatherData) {
        this.weatherData = weatherData;
    }

}

WeatherDataTemperature

代码语言:javascript
复制
import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class WeatherDataTemperature implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @ManyToOne
    @JoinColumn(name = "weather_data_id", nullable = false)
    private WeatherData weatherData;
    Double temperature;

    WeatherDataTemperature() {

    }

    public WeatherData getWeatherData() {
        return weatherData;
    }

    public void setWeatherData(WeatherData weatherData) {
        this.weatherData = weatherData;
    }

    public Double getTemperature() {
        return temperature;
    }

    public void setTemperature(Double temperature) {
        this.temperature = temperature;
    }

}

编辑

有什么方法可以让它像设计一样工作吗?在温度表中没有主键的情况下,这里还有一个JSON,说明它的外观(温度可以为单个WeaterData存储多达4个不同的值):

代码语言:javascript
复制
{
    "id": 37892,
    "date": "2020-09-15",
    "location": {
        "lat": 32.7767,
        "lon": 96.7970,
        "city": "Dallas",
        "state": "Texas"
    },
    "temperature": [
        89.7,
        84.3,
        91.2,
        93.1
    ]
}
EN

回答 1

Stack Overflow用户

发布于 2021-08-13 14:19:26

显然,要使此工作完全符合设计的要求,我必须使用@ElementCollection,如下所示:

WeatherData

代码语言:javascript
复制
import java.time.LocalDate;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class WeatherData {

    @Id
    @GeneratedValue
    Long id;
    LocalDate date;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "location_id", referencedColumnName = "id")
    Location location;

    @ElementCollection
    @CollectionTable(name = "Weather_Data_Temperature")
    private java.util.Set<Double> temperature;

    WeatherData() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public LocalDate getDate() {
        return date;
    }

    public void setDate(LocalDate date) {
        this.date = date;
    }

    public Location getLocationId() {
        return location;
    }

    public void setLocationId(Location location) {
        this.location = location;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

}

位置类没有更改

WeatherDataTemperature类磁盘(如果使用@ElementCollection则不需要)

因此,当运行项目时,h2数据库将生成良好,如下所示:

请注意,我能够插入两个不同的温度,但两者都有相同的weather_data_id,就像设计建议的那样,可以不需要主键。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68766186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档