DTOs and objects that implement the same interface

I have the following maven projects:

  • project model : I have JPA objects
  • project-rest : Spring data, Spring rest based on Spring Boot
  • project-client : Jersey clients consume the rest of the services.
  • project-web : jsf web app only
  • project-desktop : Java Fx desktop application
  • android project : a mobile app that uses my Restore web services.

I am planning to remove the JPA entities from the design model and place only DTO pojos and interfaces there and put my JPA entities in the remainder project to remove the jpa dependencies from the design model. This is because I don't want to have JPA dependencies in android - project, web - project and desktop-project .

I thought to follow the following pattern:

   @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
   {}

      

And if I need to convert from Entities to DTO use mapstruct or Selma.

But I'm not sure if this is the best practice because I have problems in my code like the following:

@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 && other.idCity != null) || (this.idCity != null && !this.idCity.equals(other.idCity)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return "com.neology.ebreeder.model.entities.City[ idCity=" + idCity + " ]";
    }

}

      

And as you can see in the entity, I have getters and setters using a common interface, and I think this might cause problems, I was thinking of overriding getters with an entity, but I cannot override setters.

I can not do it:

 @Override
    public Country getCountryId()
    {
        return countryId;
    }

      

But I cannot do this:

@Override
    public void setCountryId(Country countryId)
    {
        this.countryId = (Country) countryId;

    }

      

Do you see a better solution or can you give me your point of view :)

thank

+3


source to share


1 answer


Based on past experience, I don't think it's a good idea to use an interface that is shared between the DTO model and the JPA model.

Essentially, you tightly couple your DTO model to your JPA model in this approach.



I would prefer that they be loosely coupled and use a separate structure to copy between the two models. This would require using a metamodel (can be derived from JPA) to move and copy data from one model to another based on getters and setters.

+1


source







All Articles