I have a problem with map many-to-one relationship. When I create mapper for one-to-one, that s OK.
@Entity
@Table(name = "PATIENT")
public class PatientEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String firstName;
@OneToOne(
cascade = CascadeType.ALL,
fetch = FetchType.LAZY,
optional = false
)
private AddressEntity address;
@OneToMany(mappedBy = "patient")
private List<VisitEntity> visit;
My mapper for one-to-one relationship:
public final class PatientMapper {
public static PatientTO mapToTO(final PatientEntity patientEntity)
{
if (patientEntity == null)
{
return null;
}
final PatientTO patientTO = new PatientTO();
patientTO.setId(patientEntity.getId());
patientTO.setFirstName(patientEntity.getFirstName());
final AddressTO addressTO = new AddressTO();
addressTO.setId(patientEntity.getAddress().getId());
addressTO.setCity(patientEntity.getAddress().getCity());
patientTO.setAddress(addressTO);
return patientTO;
}
public static PatientEntity mapToEntity(final PatientTO patientTO)
{
if(patientTO == null)
{
return null;
}
PatientEntity patientEntity = new PatientEntity();
patientEntity.setId(patientTO.getId());
patientEntity.setFirstName(patientTO.getFirstName());
AddressEntity addressEntity = new AddressEntity();
addressEntity.setId(patientTO.getAddress().getId());
addressEntity.setCity(patientTO.getAddress().getCity());
patientEntity.setAddress(addressEntity);
return patientEntity;
}
}
• 如何为多到一的关系制定法典[私人名单和设计;VisitEntity> visit]?
事先感谢你的帮助。