簡體   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