传递给实体的独立实体:不允许我保存对象

时间:2018-12-21 16:58:51

标签: java spring-data-jpa

public class Certification implements Serializable {

private static final long serialVersionUID = 6045812180225694344L;

@Id
@GeneratedValue(generator = "uuid")
@Column(name = "certification_id", updatable = false, nullable = false)
private String id;

@OneToMany(cascade = CascadeType.ALL, mappedBy="certification", fetch = FetchType.EAGER, orphanRemoval = true)
private List<CertificationPrerequisite> prerequisites;

}

public class CertificationPrerequisite implements Serializable {
private static final long serialVersionUID = 6045812180225694344L;

@Id
@GeneratedValue(generator = "uuid")   
@Column(name = "id", updatable = false)
private String id;

@ManyToOne(optional = false, targetEntity = Certification.class, fetch = FetchType.EAGER)
@JoinColumn(name="certification_id", referencedColumnName = "certification_id")
private Certification certification;

@Column(name = "prerequisite_certification_id")
private String prerequisiteCertificationId;

}

当我保存包括CertificationPrerequisite的认证对象时,Id内的认证对象为空,我怀疑不允许保存该对象。有什么办法可以做得更好吗?

public interface CertificationRepository extends JpaRepository<Certification, String>, JpaSpecificationExecutor<Certification> {

}

Certification certification  =  new Certification();
CertificationPrerequisite certificationPrerequisite = new 
CertificationPrerequisite();
certificationPrerequisite.setCertification(certification);
certification.setCertificationPrerequisite(certificationPrerequisite);
CertificationRepository.save(certification);

0 个答案:

没有答案