title: DTOs and entities which implements the same interface
body: <p>I have the next maven projects:</p>
  
  <ul>
  <li><strong>project-model</strong>   : I have JPA entities</li>
  <li><strong>project-rest</strong>    : Spring data, spring rest based on spring boot</li>
  <li><strong>project-client</strong>  : Jersey clients to consume the rest services</li>
  <li><strong>project-web</strong>     : Only jsf web application</li>
  <li><strong>project-desktop</strong> : Java Fx desktop application</li>
  <li><strong>project-android</strong> : Mobile application which consumes my Rest web services.</li>
  </ul>
  
  <p>I'm planing to remove the JPA entities from the project-model and place there only DTO's pojos and interfaces and place my JPA entities in the rest project in order to remove the jpa dependencies from the project-model. This is because I don't want to have JPA dependencies in the <strong>project-android</strong>, <strong>project-web</strong> and <strong>project-desktop</strong>.</p>
  
  <p>I was thinking to follow the next schema:</p>
  
  <pre><code>   @JsonSerialize(as=CountryDto.class)
     @JsonDeserialize(as=CountryDto.class)
     public interface ICountry extends Serializable
     {}
  
     @Entity
     @Table(name = "COUNTRY")
     @JsonSerialize(as=Country.class)
     @JsonDeserialize(as=Country.class)
     public class Country implements ICountry
     {}
  
     public class CountryDto implements ICountry
     {}
  </code></pre>
  
  <p>And if I need to convert from Entities to DTO's use mapstruct or Selma.</p>
  
  <p>But I'm not sure if this is the best practice because I have problems in my code like the next:</p>
  
  <pre><code>@JsonSerialize(as=CityDto.class)
  @JsonDeserialize(as=CityDto.class)
  public interface ICity extends Serializable
  {
  
      public Integer getIdCity();
  
      public void setIdCity(Integer idCity);
  
      public String getName();
  
      public void setName(String name);
  
      public ICountry getCountryId();
  
      public void setCountryId(ICountry countryId);
  
  }
  
  
  public class CityDto implements ICity
  {
  
      private static final long serialVersionUID = -6960160473351421716L;
  
      private Integer idCity;
      private String name;
      private CountryDto countryId;
  
      public CityDto()
      {
          // TODO Auto-generated constructor stub
      }
  
      public CityDto(Integer idCity, String name, CountryDto countryId)
      {
          super();
          this.idCity = idCity;
          this.name = name;
          this.countryId = countryId;
      }
      public CityDto(Integer idCity, String name)
      {
          super();
          this.idCity = idCity;
          this.name = name;
      }
  
      @Override
      public Integer getIdCity()
      {
          return idCity;
      }
  
      @Override
      public void setIdCity(Integer idCity)
      {
          this.idCity = idCity;
      }
  
      @Override
      public String getName()
      {
          return name;
      }
  
      @Override
      public void setName(String name)
      {
          this.name = name;
      }
  
      @Override
      public ICountry getCountryId()
      {
          return countryId;
      }
  
      @Override
      public void setCountryId(ICountry countryId)
      {
          this.countryId = (CountryDto) countryId;
      }
  
  }
  
  
  @Entity
  @Table(name = "CITY")
  @JsonSerialize(as=City.class)
  @JsonDeserialize(as=City.class)
  public class City implements ICity
  {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "ID_CITY")
      private Integer idCity;
  
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 100)
      @Column(name = "NAME")
      private String name;
  
      @JoinColumn(name = "COUNTRY_ID", referencedColumnName = "ID_COUNTRY")
      @ManyToOne(optional = false)
      private Country countryId;
  
      private static final long serialVersionUID = 1L;
  
      public City()
      {
      }
  
      public City(Integer idCity)
      {
          this.idCity = idCity;
      }
  
      public City(Integer idCity, String name)
      {
          this.idCity = idCity;
          this.name = name;
      }
  
      @Override
      public Integer getIdCity()
      {
          return idCity;
      }
  
      @Override
      public void setIdCity(Integer idCity)
      {
          this.idCity = idCity;
      }
  
      @Override
      public String getName()
      {
          return name;
      }
  
      @Override
      public void setName(String name)
      {
          this.name = name;
      }
  
      @Override
      public ICountry getCountryId()
      {
          return countryId;
      }
  
      @Override
      public void setCountryId(ICountry countryId)
      {
          this.countryId = (Country) countryId;
  
      }
  
      @Override
      public int hashCode()
      {
          int hash = 0;
          hash += (idCity != null ? idCity.hashCode() : 0);
          return hash;
      }
  
      @Override
      public boolean equals(Object object)
      {
          // TODO: Warning - this method won't work in the case the id fields are
          // not set
          if (!(object instanceof City))
          {
              return false;
          }
          City other = (City) object;
          if ((this.idCity == null &amp;&amp; other.idCity != null) || (this.idCity != null &amp;&amp; !this.idCity.equals(other.idCity)))
          {
              return false;
          }
          return true;
      }
  
      @Override
      public String toString()
      {
          return "com.neology.ebreeder.model.entities.City[ idCity=" + idCity + " ]";
      }
  
  }
  </code></pre>
  
  <p>And as You can see in the entity I have getters and setters using the shared interface, and I think that It could provoke problems, I thought to override the getters using the entity but I can't override the setters.</p>
  
  <p>I cant do this:</p>
  
  <pre><code> @Override
      public Country getCountryId()
      {
          return countryId;
      }
  </code></pre>
  
  <p>But I can't do this :</p>
  
  <pre><code>@Override
      public void setCountryId(Country countryId)
      {
          this.countryId = (Country) countryId;
  
      }
  </code></pre>
  
  <p>Do you see a better solution or could you give me your point of view :)</p>
  
  <p>thanks</p>