title: Avoid repetition in many-to-one relationship
body: <p>I'm developing an application that stores the weather info for a city on a given date.
  My WeatherInfo class looks something like this:</p>
  
  <pre><code>@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&lt;Weather&gt; weather;
  
  
      @SuppressWarnings("unused")
      private WeatherInfo()
      {
      //Used by Hibernate
      }
  
      public WeatherInfo(Date aDate, City aCity,Collection&lt;Weather&gt; aWeatherCollection){
      this.date = aDate;
      this.city = aCity;
      this.weather = aWeatherCollection;
  
      }
  
  }
  </code></pre>
  
  <p>The problem is that if I save a WeatherInfo for a given city 3 times, 3 entries of the same city are added to the City table. I assumed that this would not happen if I specified a Many-to-one relationship between WeatherInfo and City. How do I stop this from happening?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I get the WeatherInfo from OpenWeatherMap REST service and map their City and Weather objects to my own.</p>
  
  <pre><code>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&lt;Weather&gt; getWeatherCollection((org.openweathermap.WeatherInfo aWeatherInfo){
  
      Collection&lt;org.openweathermap.Weather&gt; owmWeathers = aWeatherInfo.getWeathers();
      for(org.openweathermap.Weather owmWeather : owmWeathers){
          super.getWeather().add(new Weather(owmWeather.getMain(),owmWeather.getDescription()));
      }
  
      return super.getWeather();
      }
  </code></pre>