我的下一個Maven項目:的DTO和實體實現相同的接口
- 項目模型:我有JPA實體
- 項目,其餘:春季數據,彈簧安置基於春天開機
- 項目的客戶端:澤西客戶消費REST服務
- 項目的Web:只有JSF的Web應用程序
- 項目的桌面:Java的Fx的桌面應用程序
- 項目的Android:移動應用程序,它會消耗我的REST Web服務。
我刨從項目中移除模型的JPA實體,並把那裏只有DTO的POJO和接口,並把我的JPA實體,其餘的項目,以便從項目模型中刪除JPA的依賴。這是因爲我不想讓JPA依賴於project-android,project-web和project-desktop。
我想遵循下面的模式:
@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或塞爾瑪。
但我不知道這是最好的做法,因爲我有問題,在我的代碼,如未來:
@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;
}
你看到一個更好的解決辦法,或者你可以給我看你的觀點:)
謝謝
您是否真的重建過包含該接口的項目,以便其他模塊注意到新接口?不知道所有這些類所在的位置,或者您的依賴關係結構如何,或者只有JPA實體有覆蓋方法或DTO的問題 –
是的,我在重建項目,並且沒有在當前版本中我沒有接口和是的是與dtos相同的問題。我沒有附加依賴關係,因爲它們很多,但所有項目都依賴於項目模型,項目網絡和項目 - 桌面取決於項目客戶端。 –
在這一刻我有項目模型中的實體,但我要將它們移動到其餘項目中,並且我將把dto放置在模型項目中 –