我有两节课。第一类是TNota。

@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,但出现错误,因为它保留了ID = 5的新NTipoNota,该数据库已存在于数据库中。使用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);


由于此问题,我不想不必更改所有代码。当我们创建对象的新实例时,是否有任何使对象不持久化的形式?

谢谢大家

最佳答案

您的新代码正确,而您先前的代码不正确。您也可以调用合并来解决该关系。

10-04 10:26