繁体   English   中英

实现相同接口的 DTO 和实体

[英]DTOs and entities which implements the same interface

我有下一个 Maven 项目:

  • 项目模型:我有 JPA 实体
  • project-rest : Spring数据,基于spring boot的spring rest
  • project-client : Jersey 客户端使用其余服务
  • project-web : 只有 jsf web 应用程序
  • 项目桌面:Java Fx 桌面应用程序
  • project-android :使用我的 Rest Web 服务的移动应用程序。

我打算从项目模型中删除 JPA 实体,只在那里放置 DTO 的 pojo 和接口,并将我的 JPA 实体放在其余项目中,以便从项目模型中删除 jpa 依赖项。 这是因为我不想在project-androidproject-webproject-desktop 中有 JPA 依赖项。

我正在考虑遵循下一个架构:

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

如果我需要从实体转换为 DTO,请使用 mapstruct 或 Selma。

但我不确定这是否是最佳实践,因为我的代码中存在如下问题:

@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 + " ]";
    }

}

正如您在实体中看到的,我有使用共享接口的 getter 和 setter,我认为这可能会引发问题,我想使用实体覆盖 getter,但我无法覆盖 setter。

我不能这样做:

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

但我不能这样做:

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

    }

你有没有看到更好的解决方案,或者你能给我你的观点:)

谢谢

根据过去的经验,我认为使用 DTO 模型和 JPA 模型之间共享的接口不是一个好主意。

本质上,您使用这种方法将 DTO 模型与 JPA 模型紧密耦合。

我宁愿让它们松散耦合并使用单独的框架在这​​两个模型之间进行复制。 这将需要由元模型(可以从 JPA 派生)提供支持,以基于 getter 和 setter 将数据从一个模型复制到另一个模型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM