2011-10-05 2 views
0

나는 2 개의 클래스를 가지고 있습니다. 첫 번째 클래스는 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를 유지하려고하지만 이미 데이터베이스에있는 id = 5를 가진 새로운 NTipoNota를 유지하기 때문에 오류가 발생합니다.

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); 

내가 때문에이 문제에 모든 코드를 변경해야하지 싶습니다 TopLink를 사용 나는이 문제가 없었어요. 우리가 새 인스턴스를 만들 때 객체를 유지하지 못하게하는 양식이 있습니까?

고맙습니다.

답변

1

새 코드가 정확하며 이전 코드가 올바르지 않습니다. 관계를 해결하기 위해 병합을 호출 할 수도 있습니다.