我有下一个maven项目:
我正在计划从项目模型中删除JPA实体,只放置DTO的pojos和接口,并将我的JPA实体放在其余项目中,以便从项目模型中删除jpa依赖项。这是因为我不想在 project-android , project-web 和 project-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;
}
您是否看到了更好的解决方案,或者您能否给我您的观点:)
感谢
答案 0 :(得分:1)
根据过去的经验,我不认为使用DTO模型和JPA模型之间共享的界面是个好主意。
使用这种方法,您实际上是将DTO模型与JPA模型紧密结合。
我宁愿让它们松散耦合,并使用单独的框架在这两个模型之间进行复制。这需要由元模型(可以从JPA派生)驱动,以根据getter和setter将数据从一个模型移动并复制到另一个模型。