주어진 날짜에 도시의 날씨 정보를 저장하는 응용 프로그램을 개발 중입니다. 내 WeatherInfo 클래스는 다음과 같이 보입니다 :many-to-one 관계에서 반복하지 않기
@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;
}
}
문제는 내가 주어진 도시를 3 번 WeatherInfo을 저장하면, 같은 도시의 3 개 항목이 도시의 테이블에 추가되는입니다. WeatherInfo와 City 사이에 다 대일 관계를 지정하면 이런 일이 발생하지 않는다고 가정했습니다. 어떻게 이런 일이 일어나지 않게 할 수 있습니까?
UPDATE
내가 OpenWeatherMap REST 서비스에서 WeatherInfo을 얻을 내 자신의 자신의시와 날씨 객체를 매핑합니다.
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();
}
당신이 도시와 WeatherInfo 클래스의 생성자를 호출하는 코드를 보여주십시오. 내가 게시 한 코드가 아니라 문제가 있다는 것을 확신합니다. –
나는 –