我有 2 个实体,其中一个是另一个的外键:
@Entity
@Table(name = "XXXX")
@XmlRootElement
public class Drfacopt implements Serializable {
@Id
@NotNull
@Basic(optional = false)
@Column(name = "OPCOD")
private Short optionCode;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "OPCODDH")
private String optionCodeDescription;
}
@Entity
@Table(name = "YYYY")
@XmlRootElement
public class Drfac03f implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected Drfac03fPK drfac03fPK;
@JoinColumn(name = "F3OPT", referencedColumnName = "OPCOD" )
@ManyToOne(optional = false)
private Drfacopt relationCode;
}
当我尝试使用插入新记录时出现em.persist(drfac03f)
此异常:
During synchronization a new object was found through a relationship that was not marked cascade PERSIST
如何将外键插入为空?我想要@Null,所以当用户直接向 Drfacopt 插入一条记录时,它不会为 null 或零,但如果它是通过 Drfac03f 那么它可以作为 null 插入。如何才能做到这一点?