我的下一个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放置在模型项目中 –