Hi there
Im with a problem to retrieve a result from a list that is defined with a OneToMany relation. The code that raise the exception is the follow:
I have opened the session with the entityManager and a transaction. What am i forgeting to do? The problem is on the session or on the definition of the Entity? Can someone help me?
Im using Annotations to define the pojo's and the Sun JPA technology.
Hibernate version:
Hibernate Core 3.2.3
Hibernate EntityManager 3.3.1
Code:
Distrito selectedDistrito = (Distrito)distritosComboBox.getSelectedItem();
//cleaning the combo
clearConcelhosCombo();
EntityManagerFactory emf = HibernateUtil.getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
List<Concelho> concelhos = selectedDistrito.getConcelhos();
for(Concelho aConcelho: concelhos){
concelhosComboBox.addItem(aConcelho);
}
tx.commit();
em.close();
A "Distrito" is a divisory of a country in Portugal (like USA and the states)
A "Concelho" is a divisory of an "Distrito"
The Entity DistritoCode:
@Entity
@Table(name="DISTRITOS")
public class Distrito implements Serializable {
// <editor-fold defaultstate="collapsed" desc=" PrimaryKey: String id ">
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_distrito", nullable=false)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
//</editor-fold>
// <editor-fold defaultstate="collapsed" desc=" Property: String nome ">
@Column(name="nome_distrito", nullable=false, unique=true)
private String nome;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc=" N-1 Relation to Pais pais ">
@ManyToOne
@JoinColumn(name="PAIS_FK", nullable=false)
private Pais pais;
public Pais getPais() {
return this.pais;
}
public void setPais(Pais pais) {
this.pais = pais;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc=" 1-N Relation to Collection /*Concelho*/ concelhos ">
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy="distrito")
private List<Concelho> concelhos = new ArrayList<Concelho>();
public List<Concelho> getConcelhos() {
return this.concelhos;
}
public void setConcelhos(List<Concelho> concelhos) {
this.concelhos = concelhos;
}
public void addConcelho(Concelho concelho){
concelho.setDistrito(this);
concelhos.add(concelho);
}
// </editor-fold>
public String toString() {
return getNome();
}
}
The Entity ConcelhoCode:
@Entity
@Table(name="CONCELHOS")
public class Concelho implements Serializable {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_concelho", nullable=false)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="nome_concelho", nullable=false, unique=true)
private String nome;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@ManyToOne
@JoinColumn(name="DISTRITO_FK", nullable=false)
private Distrito distrito;
public Distrito getDistrito() {
return this.distrito;
}
public void setDistrito(immo.persistence.Distrito distrito) {
this.distrito = distrito;
}
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy="concelho")
private List<Freguesia> freguesias = new ArrayList<Freguesia>();
public List<Freguesia> getFreguesias() {
return this.freguesias;
}
public void setFreguesias(List<Freguesia> freguesias) {
this.freguesias = freguesias;
}
public void addFreguesia(Freguesia freguesia){
freguesia.setConcelho(this);
freguesias.add(freguesia);
}
public String toString() {
return getNome();
}
}
Thanks a lot
--ms