Estou aprendendo agora a usar o ManytoMany no hibernate,
tenho a classe Fornecedor e a classe Unidade… e relacao many to many…
como faço a inserção no banco da ligacao entre fornecedor e unidade?
tentei assim:
unidade = new GenericDAO<Unidade>(Unidade.class).pega(unidade.getCnpj()); // pego uma unidade
fornecedor.setUnidades(unidade); // ligo a unidade ao fornecedor
new GenericDAO<Fornecedor>(Fornecedor.class ).up(fornecedor);
no metodo up ele apenas da um updade…
e esta me dando o seguinte erro:
org.hibernate.HibernateException: Found two representations of same collection: br.com.geraldo.model.Fornecedor.unidades
org.hibernate.engine.Collections.processReachableCollection(Collections.java:153)
org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.java:37)
org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:131)
org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
br.com.geraldo.DAO.GenericDAO.up(GenericDAO.java:43)
br.com.geraldo.action.FornecedorAction.addUnidade(FornecedorAction.java:37)
O que esta acontecendo? estou inserindo a ligação de maneira incorreta?
estou esquecendo de algo?
[]´s
Geraldo Barboza
unidade.java
[code]import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
/*
-
Unidade(cnpj, nome, endereço(rua, número, complemento), bairro, cidade,
-
estado, telefone, referência, data de cadastro, data da ultima atualização)
*/
@Entity
public class Unidade {
public void setDataCadastro(Date dataCadastro) {
this.dataCadastro = dataCadastro;
}public Unidade() {
super();
}public Unidade(Long cnjp, String nome, String telefone) {
this.cnpj = cnjp;
this.nome = nome;
this.telefone = telefone;
this.dataCadastro = new Date();
}@Id
private Long cnpj;private String nome;
private String end;
private String bairro;
private String cidade;
private String estado;
private String telefone;
private String referencia;
private Date dataCadastro;
private Date dataUpdate;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = “Unidade_Fornecedor”, joinColumns = @JoinColumn(name = “cnpj_unidade”, referencedColumnName = “cnpj”), inverseJoinColumns = @JoinColumn(name = “cnpj_fornecedor”, referencedColumnName = “cnpj”))private java.util.Set fornecedores;
// getters and setters
}
[/code]
fornecedor.java
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
// Fornecedor(cnpj, nome, endereço(rua, número,
// complemento), bairro, cidade, estado, telefone,
// e-mail, contato, data de cadastro,
// data da ultima atualização)
@Entity
public class Fornecedor {
public void setDataCadastro(Date dataCadastro) {
this.dataCadastro = dataCadastro;
}
public Fornecedor() {
super();
}
public Fornecedor(Long cnjp, String nome, String telefone, String contato) {
this.cnpj = cnjp;
this.nome = nome;
this.telefone = telefone;
this.contato = contato;
this.dataCadastro = new Date();
}
@Id
private Long cnpj;
private String nome;
private String end;
private String bairro;
private String cidade;
private String estado;
private String telefone;
private String email;
private String contato;
private Date dataCadastro;
private Date dataUpdate;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "Unidade_Fornecedor", joinColumns = @JoinColumn(name = "cnpj_fornecedor", referencedColumnName = "cnpj"), inverseJoinColumns = @JoinColumn(name = "cnpj_unidade", referencedColumnName = "cnpj"))
private java.util.Set<Unidade> unidades;
// getters and setters