3

I made a program in java that should return all the orders made to a provider. Instead of displaying the id of the orders, it displays sakila.entity.Order@883357, where sakila.entity is the package that contains Order.java and Order.hbm.xml. I think the problem is in the method displayResult(). Maybe somebody can figure it out. Thanks a lot!

Here is the program:

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                   

}

Here is the class Order.java

package sakila.entity;

// Generated Apr 9, 2012 10:19:40 PM by Hibernate Tools 3.2.1.GA

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;
}

}

4

1 回答 1

12

Right now the System.out.println(element); line is just using the default toString() method, which prints out some internal code of the Order object.

Your Order class would need to override the toString() method for it to print out the id of the order.

Or it would probably be better to change your iterator code to read:

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

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

于 2012-05-25T00:04:02.443 回答