Hello my problem is that I can not relate two tables that are one to many, simply by executing my code in netbeans the table without foreign shows the record well, but in which contains foreign key does not show simply limited to write where the record goes the following Entity.table = registration value. That is, I have two tables, one employee with its PK that is id_employed and Sales containing the foreign one of employee_id. The problem is that when executing my java code, the following thing is shown in my Jtable and the following happens where the connection is given in the sales table writes this Entity.Employees = 1 which in itself is fine (because it gave me the value of the main table) but I wrote that of Entidades.Empleados if someone can help me my code is as follows. Greetings and have a nice day My database is made in oracle 11g and the library is EclipseLINK ! [ [] 1 This is the result.] 2 1g
This is the employee code
@Entity
@Table(name = "EMPLEADOS")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Empleados.findAll", query = "SELECT e FROM
Empleados e"),
@NamedQuery(name = "Empleados.findByIdEmpleado", query = "SELECT e FROM
Empleados e WHERE e.idEmpleado = :idEmpleado"),
@NamedQuery(name = "Empleados.findByNombre", query = "SELECT e FROM
Empleados e WHERE e.nombre = :nombre"),
@NamedQuery(name = "Empleados.findByCargo", query = "SELECT e FROM
Empleados e WHERE e.cargo = :cargo")})
public class Empleados implements Serializable {
private static final long serialVersionUID = 1L;
// @Max(value=?) @Min(value=?)//if you know range of your decimal
fields consider using these annotations to enforce field validation
@Id
@Basic(optional = false)
@Column(name = "ID_EMPLEADO")
private BigDecimal idEmpleado;
@Column(name = "NOMBRE")
private String nombre;
@Column(name = "CARGO")
private String cargo;
@OneToMany(mappedBy = "idEmpleado") //idEmpleado
private List<Ventas> ventasList;
public Empleados() {
}
public Empleados(BigDecimal idEmpleado) {
this.idEmpleado = idEmpleado;
}
public BigDecimal getIdEmpleado() {
return idEmpleado;
}
public void setIdEmpleado(BigDecimal idEmpleado) {
this.idEmpleado = idEmpleado;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getCargo() {
return cargo;
}
public void setCargo(String cargo) {
this.cargo = cargo;
}
@XmlTransient
public List<Ventas> getVentasList() {
return ventasList;
}
public void setVentasList(List<Ventas> ventasList) {
this.ventasList = ventasList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (idEmpleado != null ? idEmpleado.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 Empleados)) {
return false;
}
Empleados other = (Empleados) object;
if ((this.idEmpleado == null && other.idEmpleado != null) || (this.idEmpleado != null && !this.idEmpleado.equals(other.idEmpleado))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Entidades.Empleados[ idEmpleado=" + idEmpleado + " ]";
}
}
The sales pitch is as follows:
@Entity
@Table(name = "VENTAS")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Ventas.findAll", query = "SELECT v FROM Ventas v"),
@NamedQuery(name = "Ventas.findByNumVenta", query = "SELECT v FROM Ventas v WHERE v.numVenta = :numVenta"),
@NamedQuery(name = "Ventas.findByProducto", query = "SELECT v FROM Ventas v WHERE v.producto = :producto"),
@NamedQuery(name = "Ventas.findByPrecio", query = "SELECT v FROM Ventas v WHERE v.precio = :precio")})
public class Ventas implements Serializable {
private static final long serialVersionUID = 1L;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Id
@Basic(optional = false)
@Column(name = "NUM_VENTA")
private BigDecimal numVenta;
@Column(name = "PRODUCTO")
private String producto;
@Column(name = "PRECIO")
private BigInteger precio;
@ManyToOne()
@JoinColumn(name = "ID_EMPLEADO", referencedColumnName = "ID_EMPLEADO")
private Empleados idEmpleado;
public Ventas() {
}
public Ventas(BigDecimal numVenta) {
this.numVenta = numVenta;
}
public BigDecimal getNumVenta() {
return numVenta;
}
public void setNumVenta(BigDecimal numVenta) {
this.numVenta = numVenta;
}
public String getProducto() {
return producto;
}
public void setProducto(String producto) {
this.producto = producto;
}
public BigInteger getPrecio() {
return precio;
}
public void setPrecio(BigInteger precio) {
this.precio = precio;
}
public Empleados getIdEmpleado() {
return idEmpleado;
}
public void setIdEmpleado(Empleados idEmpleado) {
this.idEmpleado = idEmpleado;
}
@Override
public int hashCode() {
int hash = 0;
hash += (numVenta != null ? numVenta.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 Ventas)) {
return false;
}
Ventas other = (Ventas) object;
if ((this.numVenta == null && other.numVenta != null) || (this.numVenta != null && !this.numVenta.equals(other.numVenta))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Entidades.Ventas[ numVenta=" + numVenta + " ]";
}
}
Code of the graphic part
public class Interfaz extends javax.swing.JFrame {
EmpleadosJpaController cEmpleados = new EmpleadosJpaController(); // inst controlador
VentasJpaController cVentas= new VentasJpaController();
/**
* Creates new form Interfaz
*/
public Interfaz() { //todo lo que se edita aquí se ejecuta al cargar interfaz
initComponents();
CrearModelo1();
CrearModelo2();
Cargar_infoEmp();//llmar al metodo de cargar info empleado.
Cargar_infoVentas();
}
//Codigo de creacion de Jtable1 tabla empleados.
public static DefaultTableModel modelo1;
private void CrearModelo1() {
try {
modelo1 = (new DefaultTableModel(
null, new String[]{
"id_empleado", "nombre",
"cargo"}) {
Class[] types = new Class[]{
java.lang.String.class,
java.lang.String.class,
java.lang.String.class
};
boolean[] canEdit = new boolean[]{
false, false, false
};
@Override
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int colIndex) {
return canEdit[colIndex];
}
});
jTable1.setModel(modelo1);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.toString() + "error2");
}
}
//Crear tabla de ventas
public static DefaultTableModel modelo2;
private void CrearModelo2() {
try {
modelo2 = (new DefaultTableModel(
null, new String[]{
"Num_Venta", "Producto",
"Precio", "Id_empleado"}) {
Class[] types = new Class[]{
java.lang.String.class,
java.lang.String.class,
java.lang.String.class,
java.lang.String.class
};
boolean[] canEdit = new boolean[]{
false, false, false, false
};
@Override
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int colIndex) {
return canEdit[colIndex];
}
});
jTable2.setModel(modelo2);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.toString() + "error2");
}
}
private void Cargar_infoEmp() { //Para cargar informacion de la primera tabla empleados
try {
Object o[] = null;
List<Empleados> listE = cEmpleados.findEmpleadosEntities();
for (int i = 0; i < listE.size(); i++) {
modelo1.addRow(o);
modelo1.setValueAt(listE.get(i).getIdEmpleado(), i, 0);
modelo1.setValueAt(listE.get(i).getNombre(), i, 1);
modelo1.setValueAt(listE.get(i).getCargo(), i, 2);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
private void Cargar_infoVentas() { //Para cargar informacion de la 2da tabla ventas
try {
Object o[] = null;
List<Ventas> listV = cVentas.findVentasEntities();
for (int i = 0; i < listV.size(); i++) {
modelo2.addRow(o);
modelo2.setValueAt(listV.get(i).getNumVenta(), i, 0);
modelo2.setValueAt(listV.get(i).getProducto(), i, 1);
modelo2.setValueAt(listV.get(i).getPrecio(), i, 2);
modelo2.setValueAt(listV.get(i).getIdEmpleado(), i, 3);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
/**