Ola familia GUJ.
No projeto em que estou trabalhando preciso de uma relacionamento mestre detalhe no netbeans eu madei gerar um projeto de demonstração e copiei o código para o meu projeto. Aparentemente não existe erro nas classes, o projeto compila perfeitamente, mas na hora que eu clico no botão de inserir um novo detalhe que chama o metódo newDetailRecord não aparece uma nova linha na detailTable.
Por favor me deem uma luz. O que ta faltando ou ta errado?
Classe InsumosTipopackage sigc.persistencia;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
/**
*
* @author Begins
*/
@Entity
@Table(name = "insumos_tipo",schema = "")
@NamedQueries({@NamedQuery(name = "InsumosTipo.findByIdinsumoTipo", query = "SELECT i FROM InsumosTipo i WHERE i.idinsumoTipo = :idinsumoTipo"), @NamedQuery(name = "InsumosTipo.findByDescricao", query = "SELECT i FROM InsumosTipo i WHERE i.descricao = :descricao"), @NamedQuery(name = "InsumosTipo.findByTipo", query = "SELECT i FROM InsumosTipo i WHERE i.tipo = :tipo")})
public class InsumosTipo implements Serializable {
@Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id_insumoTipo", nullable = false)
private Integer idinsumoTipo;
@Column(name = "descricao", nullable = false)
private String descricao;
@Column(name = "tipo", nullable = false)
private String tipo;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "idinsumoTiposfk")
private Collection<Insumos> insumosCollection;
public InsumosTipo() {
}
public InsumosTipo(Integer idinsumoTipo) {
this.idinsumoTipo = idinsumoTipo;
}
public InsumosTipo(Integer idinsumoTipo, String descricao, String tipo) {
this.idinsumoTipo = idinsumoTipo;
this.descricao = descricao;
this.tipo = tipo;
}
public Integer getIdinsumoTipo() {
return idinsumoTipo;
}
public void setIdinsumoTipo(Integer idinsumoTipo) {
Integer oldIdinsumoTipo = this.idinsumoTipo;
this.idinsumoTipo = idinsumoTipo;
changeSupport.firePropertyChange("idinsumoTipo", oldIdinsumoTipo, idinsumoTipo);
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
String oldDescricao = this.descricao;
this.descricao = descricao;
changeSupport.firePropertyChange("descricao", oldDescricao, descricao);
}
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
String oldTipo = this.tipo;
this.tipo = tipo;
changeSupport.firePropertyChange("tipo", oldTipo, tipo);
}
public Collection<Insumos> getInsumosCollection() {
return insumosCollection;
}
public void setInsumosCollection(Collection<Insumos> insumosCollection) {
this.insumosCollection = insumosCollection;
}
@Override
public int hashCode() {
int hash = 0;
hash += (idinsumoTipo != null ? idinsumoTipo.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof InsumosTipo)) {
return false;
}
InsumosTipo other = (InsumosTipo) object;
if ((this.idinsumoTipo == null && other.idinsumoTipo != null) || (this.idinsumoTipo != null && !this.idinsumoTipo.equals(other.idinsumoTipo))) {
return false;
}
return true;
}
@Override
public String toString() {
return "d1.InsumosTipo[idinsumoTipo=" + idinsumoTipo + "]";
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
}
package sigc.persistencia;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
/**
*
* @author Begins
*/
@Entity
@Table(name = "insumos",schema = "")
@NamedQueries({@NamedQuery(name = "Insumos.findByIdInsumo", query = "SELECT i FROM Insumos i WHERE i.idInsumo = :idInsumo"), @NamedQuery(name = "Insumos.findByInsumo", query = "SELECT i FROM Insumos i WHERE i.insumo = :insumo"), @NamedQuery(name = "Insumos.findByDescricao", query = "SELECT i FROM Insumos i WHERE i.descricao = :descricao"), @NamedQuery(name = "Insumos.findByValor", query = "SELECT i FROM Insumos i WHERE i.valor = :valor"), @NamedQuery(name = "Insumos.findByTipo", query = "SELECT i FROM Insumos i WHERE i.tipo = :tipo"), @NamedQuery(name = "Insumos.findByUn", query = "SELECT i FROM Insumos i WHERE i.un = :un")})
public class Insumos implements Serializable {
@Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id_insumo", nullable = false)
private Integer idInsumo;
@Column(name = "insumo", nullable = false)
private String insumo;
@Column(name = "descricao", nullable = false)
private String descricao;
@Column(name = "valor")
private BigDecimal valor;
@Column(name = "tipo", nullable = false)
private String tipo;
@Column(name = "un")
private String un;
@JoinColumn(name = "id_insumoTipos_fk", referencedColumnName = "id_insumoTipo")
@ManyToOne
private InsumosTipo idinsumoTiposfk;
public Insumos() {
}
public Insumos(Integer idInsumo) {
this.idInsumo = idInsumo;
}
public Insumos(Integer idInsumo, String insumo, String descricao, String tipo) {
this.idInsumo = idInsumo;
this.insumo = insumo;
this.descricao = descricao;
this.tipo = tipo;
}
public Integer getIdInsumo() {
return idInsumo;
}
public void setIdInsumo(Integer idInsumo) {
Integer oldIdInsumo = this.idInsumo;
this.idInsumo = idInsumo;
changeSupport.firePropertyChange("idInsumo", oldIdInsumo, idInsumo);
}
public String getInsumo() {
return insumo;
}
public void setInsumo(String insumo) {
String oldInsumo = this.insumo;
this.insumo = insumo;
changeSupport.firePropertyChange("insumo", oldInsumo, insumo);
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
String oldDescricao = this.descricao;
this.descricao = descricao;
changeSupport.firePropertyChange("descricao", oldDescricao, descricao);
}
public BigDecimal getValor() {
return valor;
}
public void setValor(BigDecimal valor) {
BigDecimal oldValor = this.valor;
this.valor = valor;
changeSupport.firePropertyChange("valor", oldValor, valor);
}
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
String oldTipo = this.tipo;
this.tipo = tipo;
changeSupport.firePropertyChange("tipo", oldTipo, tipo);
}
public String getUn() {
return un;
}
public void setUn(String un) {
String oldUn = this.un;
this.un = un;
changeSupport.firePropertyChange("un", oldUn, un);
}
public InsumosTipo getIdinsumoTiposfk() {
return idinsumoTiposfk;
}
public void setIdinsumoTiposfk(InsumosTipo idinsumoTiposfk) {
InsumosTipo oldIdinsumoTiposfk = this.idinsumoTiposfk;
this.idinsumoTiposfk = idinsumoTiposfk;
changeSupport.firePropertyChange("idinsumoTiposfk", oldIdinsumoTiposfk, idinsumoTiposfk);
}
@Override
public int hashCode() {
int hash = 0;
hash += (idInsumo != null ? idInsumo.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Insumos)) {
return false;
}
Insumos other = (Insumos) object;
if ((this.idInsumo == null && other.idInsumo != null) || (this.idInsumo != null && !this.idInsumo.equals(other.idInsumo))) {
return false;
}
return true;
}
@Override
public String toString() {
return "d1.Insumos[idInsumo=" + idInsumo + "]";
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
}
package sigc.cadastrar;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jdesktop.application.Action;
import org.jdesktop.application.Task;
import org.jdesktop.beansbinding.AbstractBindingListener;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.PropertyStateEvent;
import sigc.persistencia.Insumos;
import sigc.persistencia.InsumosTipo;
/**
*
* @author Begins
*/
public class CadInsumos extends javax.swing.JDialog {
/** Creates new form CadInsumos */
public CadInsumos(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
// tracking table selection
masterTable.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
firePropertyChange("recordSelected", !isRecordSelected(), isRecordSelected());
}
});
detailTable.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
firePropertyChange("detailRecordSelected", !isDetailRecordSelected(), isDetailRecordSelected());
}
});
// tracking changes to save
bindingGroup.addBindingListener(new AbstractBindingListener() {
@Override
public void targetChanged(Binding binding, PropertyStateEvent event) {
// save action observes saveNeeded property
setSaveNeeded(true);
}
});
// have a transaction started
entityManager.getTransaction().begin();
}
public boolean isSaveNeeded() {
return saveNeeded;
}
private void setSaveNeeded(boolean saveNeeded) {
if (saveNeeded != this.saveNeeded) {
this.saveNeeded = saveNeeded;
firePropertyChange("saveNeeded", !saveNeeded, saveNeeded);
}
}
public boolean isRecordSelected() {
return masterTable.getSelectedRow() != -1;
}
public boolean isDetailRecordSelected() {
return detailTable.getSelectedRow() != -1;
}
@Action
public void newRecord() {
InsumosTipo I = new InsumosTipo();
entityManager.persist(I);
list.add(I);
int row = list.size()-1;
masterTable.setRowSelectionInterval(row, row);
masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
setSaveNeeded(true);
}
@Action(enabledProperty = "recordSelected")
public void deleteRecord() {
int[] selected = masterTable.getSelectedRows();
List<InsumosTipo> toRemove = new ArrayList<InsumosTipo>(selected.length);
for (int idx=0; idx<selected.length; idx++) {
InsumosTipo I = list.get(masterTable.convertRowIndexToModel(selected[idx]));
toRemove.add(I);
entityManager.remove(I);
}
list.removeAll(toRemove);
setSaveNeeded(true);
}
@Action(enabledProperty = "recordSelected")
public void newDetailRecord() {
int index = masterTable.getSelectedRow();
InsumosTipo I = list.get(masterTable.convertRowIndexToModel(index));
Collection<Insumos> is = I.getInsumosCollection();
if (is == null) {
is = new LinkedList<Insumos>();
I.setInsumosCollection(is);
}
Insumos i = new Insumos();
entityManager.persist(i);
i.setIdinsumoTiposfk(I);
is.add(i);
masterTable.clearSelection();
masterTable.setRowSelectionInterval(index, index);
int row = is.size()-1;
detailTable.setRowSelectionInterval(row, row);
detailTable.scrollRectToVisible(detailTable.getCellRect(row, 0, true));
setSaveNeeded(true);
}
@Action(enabledProperty = "detailRecordSelected")
public void deleteDetailRecord() {
int index = masterTable.getSelectedRow();
InsumosTipo I = list.get(masterTable.convertRowIndexToModel(index));
Collection<Insumos> is = I.getInsumosCollection();
int[] selected = detailTable.getSelectedRows();
List<Insumos> toRemove = new ArrayList<Insumos>(selected.length);
for (int idx=0; idx<selected.length; idx++) {
selected[idx] = detailTable.convertRowIndexToModel(selected[idx]);
int count = 0;
Iterator<Insumos> iter = is.iterator();
while (count++ < selected[idx]) iter.next();
Insumos i = iter.next();
toRemove.add(i);
entityManager.remove(i);
}
is.removeAll(toRemove);
masterTable.clearSelection();
masterTable.setRowSelectionInterval(index, index);
setSaveNeeded(true);
}
@Action(enabledProperty = "saveNeeded")
public Task save() {
return new SaveTask(sigc.SiGCApp.getApplication());
}
private class SaveTask extends Task {
SaveTask(org.jdesktop.application.Application app) {
super(app);
}
@Override protected Void doInBackground() {
try {
entityManager.getTransaction().commit();
entityManager.getTransaction().begin();
} catch (RollbackException rex) {
rex.printStackTrace();
entityManager.getTransaction().begin();
List<InsumosTipo> merged = new ArrayList<InsumosTipo>(list.size());
for (InsumosTipo I : list) {
merged.add(entityManager.merge(I));
}
list.clear();
list.addAll(merged);
}
return null;
}
@Override protected void finished() {
setSaveNeeded(false);
}
}
/**
* An example action method showing how to create asynchronous tasks
* (running on background) and how to show their progress. Note the
* artificial 'Thread.sleep' calls making the task long enough to see the
* progress visualization - remove the sleeps for real application.
*/
@Action
public Task refresh() {
return new RefreshTask(sigc.SiGCApp.getApplication());
}
private class RefreshTask extends Task {
RefreshTask(org.jdesktop.application.Application app) {
super(app);
}
@SuppressWarnings("unchecked")
@Override protected Void doInBackground() {
try {
setProgress(0, 0, 4);
setMessage("Rolling back the current changes...");
setProgress(1, 0, 4);
entityManager.getTransaction().rollback();
Thread.sleep(1000L); // remove for real app
setProgress(2, 0, 4);
setMessage("Starting a new transaction...");
entityManager.getTransaction().begin();
Thread.sleep(500L); // remove for real app
setProgress(3, 0, 4);
setMessage("Fetching new data...");
java.util.Collection data = query.getResultList();
for (Object entity : data) {
entityManager.refresh(entity);
}
Thread.sleep(1300L); // remove for real app
setProgress(4, 0, 4);
Thread.sleep(150L); // remove for real app
list.clear();
list.addAll(data);
} catch(InterruptedException ignore) { }
return null;
}
@Override protected void finished() {
setMessage("Done.");
setSaveNeeded(false);
}
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("sgcPU").createEntityManager();
query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT i FROM InsumosTipo i");
list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
jScrollPane1 = new javax.swing.JScrollPane();
masterTable = new javax.swing.JTable();
jScrollPane2 = new javax.swing.JScrollPane();
detailTable = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setName("Form"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
masterTable.setName("masterTable"); // NOI18N
org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${descricao}"));
columnBinding.setColumnName("Descricao");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tipo}"));
columnBinding.setColumnName("Tipo");
columnBinding.setColumnClass(String.class);
bindingGroup.addBinding(jTableBinding);
jTableBinding.bind();
jScrollPane1.setViewportView(masterTable);
jScrollPane2.setName("jScrollPane2"); // NOI18N
detailTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
detailTable.setName("detailTable"); // NOI18N
org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${selectedElement.insumosCollection}");
jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, eLProperty, detailTable);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${insumo}"));
columnBinding.setColumnName("Insumo");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${descricao}"));
columnBinding.setColumnName("Descricao");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tipo}"));
columnBinding.setColumnName("Tipo");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${un}"));
columnBinding.setColumnName("Un");
columnBinding.setColumnClass(String.class);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${valor}"));
columnBinding.setColumnName("Valor");
columnBinding.setColumnClass(java.math.BigDecimal.class);
bindingGroup.addBinding(jTableBinding);
jTableBinding.bind();
jScrollPane2.setViewportView(detailTable);
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(sigc.SiGCApp.class).getContext().getActionMap(CadInsumos.class, this);
jButton1.setAction(actionMap.get("newRecord")); // NOI18N
jButton1.setName("jButton1"); // NOI18N
jButton2.setAction(actionMap.get("deleteRecord")); // NOI18N
jButton2.setName("jButton2"); // NOI18N
jButton3.setAction(actionMap.get("newDetailRecord")); // NOI18N
jButton3.setName("jButton3"); // NOI18N
jButton4.setAction(actionMap.get("deleteDetailRecord")); // NOI18N
jButton4.setName("jButton4"); // NOI18N
jButton5.setAction(actionMap.get("save")); // NOI18N
jButton5.setName("jButton5"); // NOI18N
jButton6.setAction(actionMap.get("refresh")); // NOI18N
jButton6.setName("jButton6"); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
.addComponent(jButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6))
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE))
.addGap(18, 18, 18))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton4)
.addComponent(jButton5)
.addComponent(jButton6))
.addContainerGap(149, Short.MAX_VALUE))
);
bindingGroup.bind();
pack();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JTable detailTable;
private javax.persistence.EntityManager entityManager;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private java.util.List<sigc.persistencia.InsumosTipo> list;
private javax.swing.JTable masterTable;
private javax.persistence.Query query;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration
private boolean saveNeeded;
}