2014-08-31 1 views
0

주어진 날짜에 도시의 날씨 정보를 저장하는 응용 프로그램을 개발 중입니다. 내 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(); 
    } 
+0

당신이 도시와 WeatherInfo 클래스의 생성자를 호출하는 코드를 보여주십시오. 내가 게시 한 코드가 아니라 문제가 있다는 것을 확신합니다. –

+0

나는 –

답변

1

귀하의 getCity(WeatherInfo) 방법은 새로운 도시가 호출 될 때마다 반환합니다. 즉, 3 회 호출하면 3 도시가 반환되어 결국 CITY 테이블에 3 행이 표시됩니다. getCity(WeatherInfo)이 동일한 도시 3 개를 반환하면 여전히 3 개의 다른 대상이되므로 CITY 테이블에서 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; 
} 
+0

코드를 첨부했습니다. 감사합니다 :) check-city-exists 코드를 DAO 클래스의 saveWeatherInfo 메소드에 통합했습니다. –

관련 문제