2012-05-25 50 views
3

我在java中製作了一個程序,該程序應該返回對提供程序所做的所有訂單。 而不是顯示訂單的ID,它顯示[email protected],其中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; 

//生成2012年4月9日下午10點19分四十秒由Hibernate工具3.2.1.GA

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

公共類訂單眼下System.out.println(element);線只使用默認toString()方法,能打印出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; 
} 

}

+0

您是否可以發佈Order.java的代碼(如果我的回答沒有給您足夠的幫助)? – matt

+0

對象的默認'toString()'表示是對象的完全限定類名,後跟@,後跟對象的hashCode。所以你看到的是你應該看到的東西,除非你給對象的類一個體面的'public String toString()'方法,它返回一個字符串,顯示你想要對象顯示的信息。 1+ to @matt的正確和簡潔的答案。 –

+0

@matt你有一個想法,我可以如何使用toString()方法,以查看我的ID顯示,而不是哈希碼?也許你有時間可以幫助我。謝謝! – alin

回答

12

您的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

+0

感謝您的建議@matt。但是,如果你能告訴我如何在我的例子中使用toString()方法,我將不勝感激。我也發佈了Order.java。 – alin

+0

用更多信息和更好的方式進行編輯。 – matt

相關問題