简体   繁体   English

一对多关系中的实体仅在应用程序重新启动后可见(JPA和PostgreSQL)

[英]Entities from a One-To-Many relation only visible after application restart (JPA & PostgreSQL)

I have a problem with entity relationships using the JPA. 我对使用JPA的实体关系有疑问。

I have these two JPA Entities. 我有这两个JPA实体。 Both tables are created by the application when the application boots up using <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> 当应用程序使用<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>启动时,应用程序会创建两个表

@Entity(name = "patient")
public class Patient {

    @XmlID
    @XmlElement
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false, nullable = false)
    private Integer id;

    private Integer height;

    private Integer weight;

    @XmlJavaTypeAdapter(YearAdapter.class)
    @Temporal(TemporalType.TIMESTAMP)
    private Date yearOfBirth;

    private Sex sex;

    @OneToMany(mappedBy = "patient", fetch = FetchType.LAZY)
    private List<Sample> samples;

   // Getters and Setters omitted
}

@Entity(name = "sample")
public class Sample {

    @XmlID
    @XmlElement
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false, nullable = false)
    private Integer id;

    private boolean aliquotated;

    private BigDecimal initialVolume;

    private BigDecimal availableVolume;

    @XmlIDREF
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "patient_id")
    private Patient patient;

   // Getters and Setters omitted
}

When adding new Samples using these two methods 使用这两种方法添加新样本时

@PersistenceContext(unitName = "postgres-hcc")
private EntityManager em;

/* ... */

@Transactional
public Sample storeSample(Sample sample) {

    sample.setAvailableVolume(sample.getInitialVolume());

    em.persist(sample);

    return sample;
}

@Transactional
public Sample storeSample(SampleDTO sampleDTO) {

    Sample sample = new Sample();

    sample.setInitialVolume(sampleDTO.getInitialVolume());
    sample.setAliquotated(sampleDTO.isAliquotated());
    sample.setPatient(patientService.getPatient(sampleDTO.getPatientId()));

    return this.storeSample(sample);
}

new samples are not visible when fetching Patients; 提取患者时看不到新样品; the samples List is empty. samples列表为空。 They are written to the database though. 但是它们被写入数据库。 Also, after restarting the application without the drop-and-create option, all samples (that were written to the DB) become visible in the samples list. 同样,在不带drop-and-create选项的情况下重新启动应用程序之后,所有示例(已写入DB)都在samples列表中可见。

I have a feeling this has to do with how the linked Samples are fetched and are not re-fetched once new Samples are added to a patient. 我觉得这与在将新样本添加到患者后如何获取链接的样本以及如何不重新获取链接的样本有关。 Hence they appear after the application restarts. 因此,它们在应用程序重启后出现。

Is there anything that can be done in this case or are significant changes required? 在这种情况下是否可以做任何事情,或者需要进行重大更改?


ADDITIONAL INFO Fetching the patient(s): 其他信息获取患者:

public List<Patient> getPatients() {
    TypedQuery<Patient> q = em.createQuery("SELECT p FROM patient p", Patient.class);

    if (q == null) {
        return null;
    }

    return q.getResultList();
}

public Patient getPatient(Integer patientId) {
    Patient patient = em.find(Patient.class, patientId);

    if (patient != null) {
        return patient;
    } else {
        throw new NotFoundException("No patient with id " + patientId);
    }
}

There is no such thing as automatic re-fetch. 没有自动重新获取之类的东西。

You must add the Sample to the Patient not only the Patient to the sample. 您不仅要将患者添加到样本中,还必须将样本添加到患者中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM