Hibernate 避免多对一关系中的重复
我正在开发一个应用程序,用于存储给定日期城市的天气信息。 我的WeatherInfo类如下所示:Hibernate 避免多对一关系中的重复,hibernate,Hibernate,我正在开发一个应用程序,用于存储给定日期城市的天气信息。 我的WeatherInfo类如下所示: @Entity public class WeatherInfo implements ResponseObject { @Id @GeneratedValue private int id; @Column @Temporal(value=TemporalType.TIMESTAMP) @JsonProperty("date") @Json
@Entity
public class WeatherInfo implements ResponseObject
{
@Id
@GeneratedValue
private int id;
@Column
@Temporal(value=TemporalType.TIMESTAMP)
@JsonProperty("date")
@JsonSerialize(using=JsonUtils.TimestampSerializer.class)
private Date date;
@ManyToOne(cascade=CascadeType.ALL)
@JsonProperty("city")
private City city;
@ManyToMany(cascade=CascadeType.ALL)
@JsonProperty("weather")
private Collection<Weather> weather;
@SuppressWarnings("unused")
private WeatherInfo()
{
//Used by Hibernate
}
public WeatherInfo(Date aDate, City aCity,Collection<Weather> aWeatherCollection){
this.date = aDate;
this.city = aCity;
this.weather = aWeatherCollection;
}
}
@实体
公共类WeatherInfo实现ResponseObject
{
@身份证
@生成值
私有int-id;
@纵队
@时态(值=TemporalType.TIMESTAMP)
@JsonProperty(“日期”)
@JsonSerialize(使用=JsonUtils.TimestampSerializer.class)
私人日期;
@多通(级联=级联类型.ALL)
@JsonProperty(“城市”)
私人城市;
@多个(级联=级联类型.ALL)
@JsonProperty(“天气”)
私人收藏天气;
@抑制警告(“未使用”)
私人天气资讯(
{
//由Hibernate使用
}
公共天气信息(日期、城市、收集和其他收集){
this.date=aDate;
这个城市=城市;
this.weather=aWeatherCollection;
}
}
问题是,如果我为给定城市保存WeatherInfo 3次,同一城市的3个条目将添加到城市表中。我假设如果我指定WeatherInfo和City之间的多对一关系,就不会发生这种情况。我该如何阻止这种情况发生
更新
我从OpenWeatherMapREST服务获得天气信息,并将他们的城市和天气对象映射到我自己的地图上
public OWMWeatherInfoAdapter(org.openweathermap.WeatherInfo aWeatherInfo)
{
super();
super.setDate(aWeatherInfo.getTimestamp());
super.setCity(getCity(aWeatherInfo));
super.setWeather(getWeatherCollection(aWeatherInfo));
}
private City getCity((org.openweathermap.WeatherInfo aWeatherInfo){
Synopsis sys = aWeatherInfo.getSynopsis();
Coordinate coord = new Coordinate(aWeatherInfo.getCoordinates().getLongitude(),aWeatherInfo.getCoordinates().getLatitude());
return new City(aWeatherInfo.getCityName(),sys.getCountry(),sys.getSunrise(),sys.getSunset(),coord);
}
private Collection<Weather> getWeatherCollection((org.openweathermap.WeatherInfo aWeatherInfo){
Collection<org.openweathermap.Weather> owmWeathers = aWeatherInfo.getWeathers();
for(org.openweathermap.Weather owmWeather : owmWeathers){
super.getWeather().add(new Weather(owmWeather.getMain(),owmWeather.getDescription()));
}
return super.getWeather();
}
public OWMWeatherInfoAdapter(org.openweathermap.WeatherInfo aWeatherInfo)
{
超级();
super.setDate(aWeatherInfo.getTimestamp());
super.setCity(getCity(aWeatherInfo));
super.setWeather(getWeatherCollection(aWeatherInfo));
}
私人城市getCity((org.openweathermap.WeatherInfo aWeatherInfo){
Synopsis sys=aWeatherInfo.getSynopsis();
坐标坐标=新坐标(aWeatherInfo.getCoordinates().getLongitude(),aWeatherInfo.getCoordinates().getLatitude());
返回新城市(aWeatherInfo.getCityName()、sys.getCountry()、sys.getSunrise()、sys.getSunset()、coord);
}
私人收藏getWeatherCollection((org.openweathermap.WeatherInfo aWeatherInfo){
集合owmWeathers=aWeatherInfo.getWeathers();
用于(org.openweathermap.Weather owmWeather:owmWeather){
super.getWeather().add(新天气(owmWeather.getMain(),owmWeather.getDescription());
}
返回super.getWeather();
}
您的getCity(WeatherInfo)
方法每次调用都返回一个新城市。这意味着如果调用3次,它将返回3个不同的城市,最终导致城市表中的3行。如果getCity(WeatherInfo)
返回3个相同的城市,它仍然是3个不同的对象,因此城市表中有3行
为了避免这种情况,您需要在创建新城市之前检查给定城市的实例是否已存在。如果您可以假设没有两个城市可以共享同一名称,则可以首先在DB中按城市名称查找城市:
private getCity(WeatherInfo info) {
Query query = session.createQuery("from City where name = :name"); // assume field session is available
query.setParameter("name", info.getCityName());
City city = query.uniqueResult();
if (city == null) {
// Only in this case should we create a new City
Synopsis sys = info.getSynopsis();
Coordinate coord = new Coordinate(iherInfo.getCoordinates().getLongitude(), info.getCoordinates().getLatitude());
city = new City(info.getCityName(), sys.getContry(), sys.getSunrise(), coord);
session.save(city);
}
return city;
}
请在调用City和WeatherInfo类的构造函数的地方显示代码。我很确定问题在那里,而不是在您发布的代码中。我已经附上了有效的代码,谢谢:)我在DAO类的saveWeatherInfo方法中集成了检查City存在的代码。