Java – the program does not display the desired string representation of the object

I created a program in Java that should return all orders to the provider

This is the procedure:

package sakila.ui;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import sakila.entity.Comanda;
import sakila.entity.Furnizor;
import sakila.util.HibernateUtil;


public class SearchOrders extends javax.swing.JFrame {

public SearchOrders() {
    initComponents();
}


private void queryButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                             
    //if (!idProviderTextField.getText().trim().equals(""))
            runQueryBasedOnIdFurnizor();
}                                            

private void runQueryBasedOnIdProvider(){
    executeHQLQuery("from Provider c where c.idprovider like '" +  idProvider.getText() + "%'" );

}

 private void executeHQLQuery(String hql){
    try{
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    org.hibernate.Query q = session.createQuery(hql);
    List resultList = q.list();
    displayResult(resultList);
    session.getTransaction().commit();
    }catch (HibernateException he){
        he.printStackTrace();
    }
}

  private void displayResult(List resultList){
    Vector<String> tableHeaders = new Vector<String>();
    Vector tableData = new Vector();
    tableHeaders.add("IdOrder");

    for (Object o : resultList){
        Provider p = (Provider) o;

        Set<Order> c = p.getOrder();
        Iterator it = c.iterator();
        while (it.hasNext()) {
            Object element = it.next();
            System.out.println(element);
            Vector <Object> oneRow = new Vector <Object>();
            oneRow.add(element);

        tableData.add(oneRow);
        }
    }
   resultTable1.setModel(new DefaultTableModel(tableData,tableHeaders)); 
}


public static void main(String args[]) {
        public void run() {
            new SearchOrders().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JTextField idProviderTextField;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton queryButton1;
private javax.swing.JTable resultTable1;
// End of variables declaration

}

This is order Java class

package sakila.entity;

//By hibernate tools 3.2 1. GA was generated at 10:19:40 pm on April 9, 2012

import java. util. HashSet; import java. util. Set;

Public class order implements Java io. Serializable {

private int idorder;
 private Client client;
 private Depozit warehouse;
 private Furnizor provider;
 private Integer idproduct;
 private Integer unitmas;
 private Integer quantity;
 private Set comdetals = new HashSet(0);
 private Set facturas = new HashSet(0);

public Order() {
}


public Order(int idorder) {
    this.idorder = idorder;
}
public Order(int idorder,Client client,Warehouse warehouse,Provider provider,Integer idproduct,Integer unitmas,Integer quantity,Set comdetals,Set facturas) {
   this.idorder = idorder;
   this.client = client;
   this.warehouse = warehouse;
   this.provider = provider;
   this.idproduct = idproduct;
   this.unitmas = unitmas;
   this.quantity = quantity;
   this.comdetals = comdetals;
   this.facturas = facturas;
}

public int getIdorder() {
    return this.idorder;
}

public void setIdorder(int idorder) {
    this.idorder = idorder;
}
public Client getClient() {
    return this.client;
}

public void setClient(Client client) {
    this.client = client;
}
public Warehouse getWarehouse() {
    return this.warehouse;
}

public void setWarehouse(Warehouse warehouse) {
    this.warehouse = warehouse;
}
public Provider getProvider() {
    return this.provider;
}

public void setProvider(Provider provider) {
    this.provider = provider;
}
public Integer getIdproduct() {
    return this.idproduct;
}

public void setIdproduct(Integer idproduct) {
    this.idproduct = idproduct;
}
public Integer getUnitmas() {
    return this.unitmas;
}

public void setUnitmas(Integer unitmas) {
    this.unitmas = unitmas;
}
public Integer getQuantity() {
    return this.quantity;
}

public void setQuantity(Integer quantity){
    this.quantity = quantity;
}
public Set getComdetals() {
    return this.comdetals;
}

public void setComdetals(Set comdetals) {
    this.comdetals = comdetals;
}
public Set getFacturas() {
    return this.facturas;
}

public void setFacturas(Set facturas) {
    this.facturas = facturas;
}

}

Solution

Now it's system out. println(element); Line just uses the default toString () method, which prints out some internal code of the order object

Your order class needs to override the toString () method to print out the ID of the order

Or change the iterator code to better:

Iterator<Order> it = c.iterator();
while (it.hasNext()) {
    Order element = it.next();
    System.out.println(element.getIdorder());

So you don't have to write a toString () method for this case Typically, when you override tostring(), you want it to print out the text representation of the complete object: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#toString%28%29

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>