0
我有兩個分類。第一堂課是TNota。EclipseLink中的CASCADETYPE JPA
@Entity
@Table(name = "t_nota")
public class TNota implements Serializable {
@Id
@SequenceGenerator(name="seq_t_nota", sequenceName="seq_t_nota", initialValue=37, allocationSize=1)
@GeneratedValue(generator="seq_t_nota")
@Basic(optional = false)
@Column(name = "id_nota", nullable = false)
private double idNota;
@Basic(optional = false)
@Column(name = "nota", nullable = false, length = 255)
private String nota;
@JoinColumn(name = "id_tipo_nota", referencedColumnName = "id", nullable = false)
@ManyToOne(optional = false)
private NTipoNota nTipoNota;
public TNota() {
}
public TNota(Long idNota) {
this.idNota = idNota;
}
public double getIdNota() {
return idNota;
}
public void setIdNota(double idNota) {
this.idNota = idNota;
}
public String getNota() {
return nota;
}
public void setNota(String nota) {
this.nota = nota;
}
public NTipoNota getNTipoNota() {
return nTipoNota;
}
public void setNTipoNota(NTipoNota nTipoNota) {
this.nTipoNota = nTipoNota;
}
}
和其他類NtipoNota ..
@Entity
@Table(name = "n_tipo_nota")
public class NTipoNota implements Serializable {
@Id
@Basic(optional = false)
@Column(name = "id", nullable = false)
private Integer id;
@Basic(optional = false)
@Column(name = "nombre", nullable = false, length = 255)
private String nombre;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "nTipoNota",fetch=FetchType.LAZY)
private List<TNota> tNotaList;
public NTipoNota() {
}
public NTipoNota(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public List<TNota> getTNotaList() {
return tNotaList;
}
public void setTNotaList(List<TNota> tNotaList) {
this.tNotaList = tNotaList;
}
}
我擁有所有類型的存儲在數據庫中的筆記。我只是想堅持一個新的TNota如下,但我得到一個錯誤,因爲它堅持一個新的NTipoNota與id = 5已經存在於數據庫中。使用的TopLink我從未有過這樣的煩惱:
TNota note = new TNota();
note.setNota("Hola mundo");
note.setNTipoNota(new NTipoNota(5));
manager.persist(note);
我確定如下:
TNota note = new TNota();
note.setNota("Hola mundo");
note.setNTipoNota(manager.find(NTipoNota.class, 5);
manager.persist(note);
我想不會有所有代碼變化,由於這個問題。有什麼形式可以讓我們在創建對象的新實例時不會堅持對象?
謝謝大家。