English 中文(简体)
如何规划多到一的关系?
原标题:How to map many-to-one relationship?

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]?

事先感谢你的帮助。

问题回答

You need to map the List<VisitEntity> in your PatientEntity to List<VisitTO> in your PatientTO and vice-versa. I m assuming you also have a VisitMapper that maps between VisitEntity and VisitTO.

例如:

import java.util.List;
import java.util.stream.Collectors;

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());
        
        // Map VisitEntity list to VisitTO list
        List<VisitTO> visitTOs = patientEntity.getVisits()
            .stream()
            .map(VisitMapper::mapToTO)
            .collect(Collectors.toList());
        patientTO.setVisits(visitTOs);

        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());
        
        // Map VisitTO list to VisitEntity list
        List<VisitEntity> visitEntities = patientTO.getVisits()
            .stream()
            .map(VisitMapper::mapToEntity)
            .collect(Collectors.toList());
        patientEntity.setVisits(visitEntities);

        patientEntity.setAddress(addressEntity);

        return patientEntity;
    }
}

这一修改假定你有<条码>。 访问Mapper 类别:ToTO 。 访问:<>。 如果你不这样做,那么你将需要与您的“条码”相类似。 AddressMapper

Still, remember to set the patient in each VisitEntity when you are mapping from VisitTO to VisitEntity, otherwise you will lose the link back to the patient.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签