程序不显示对象的所需字符串表示

时间:2012-05-25 00:00:32

标签: java

我在java中创建了一个程序,该程序应该返回提供者的所有订单。 它不显示订单的ID,而是显示sakila.entity.Order@883357,其中sakila.entity是包含Order.java和Order.hbm.xml的包。 我认为问题在于方法displayResult()。也许有人可以搞清楚。 非常感谢!

以下是该计划:

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                   

}

这是Order.java类

package sakila.entity;

//由Hibernate Tools 3.2.1.GA

于2012年4月9日下午10:19:40生成

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

public class Order实现了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;
}

}

1 个答案:

答案 0 :(得分:12)

现在System.out.println(element);行只使用默认的toString()方法,该方法打印出Order对象的一些内部代码。

您的Order课程需要覆盖toString()方法才能打印出订单的ID。

或者将迭代器代码更改为:

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

这样你就不必为这一个案例编写toString()方法。通常当您覆盖toString()时,您希望它打印出完整对象的文本表示:http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#toString%28%29