0

我试图在我的名为 customer 的表中显示所有行,但它正在迭代同一行。

例如:我有两行具有不同的值,但第一行打印了两次。请多多指教。

public List listCustomers(String cifNumber_){
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    Customer cntct = null;
    try{
         tx = session.beginTransaction();
         Query sql = session.createQuery("from Customer");
         List cntctList = sql.list();          
         Object obj[] = cntctList.toArray();
         for(int i=0; i<obj.length; i++)
         {
     System.out.println("length " +obj.length);
     cntct = (Customer)obj[i]; 
     System.out.println(cntct.getId()); 
     System.out.println(cntct.getCifNumber());
     System.out.println(cntct.getCustomerName());
     System.out.println(cntct.getIdCountry());
                } 

客户.java

package com.java.vinoth;

public class CustomerSearchActionForm extends org.apache.struts.action.ActionForm{
private int id;
private String customerName;
private String cifNumber;
private int idNumber;
private String idCountry;
private String idType;
private int master_id;
private String rmCode;
private String customerCountry;
private String message;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getCustomerName() {
    return customerName;
}
public void setCustomerName(String customerName) {
    this.customerName = customerName;
}
public String getCifNumber() {
    return cifNumber;
}
public void setCifNumber(String cifNumber) {
    this.cifNumber = cifNumber;
}
public int getIdNumber() {
    return idNumber;
}
public void setIdNumber(int idNumber) {
    this.idNumber = idNumber;
}
public String getIdCountry() {
    return idCountry;
}
public void setIdCountry(String idCountry) {
    this.idCountry = idCountry;
}
public String getIdType() {
    return idType;
}
public void setIdType(String idType) {
    this.idType = idType;
}
public int getMaster_id() {
    return master_id;
}
public void setMaster_id(int master_id) {
    this.master_id = master_id;
}
public String getRmCode() {
    return rmCode;
}
public void setRmCode(String rmCode) {
    this.rmCode = rmCode;
}
public String getCustomerCountry() {
    return customerCountry;
}
public void setCustomerCountry(String customerCountry) {
    this.customerCountry = customerCountry;
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}


}

休眠.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
    <property name="hbm2ddl.auto">update</property>
    <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
  <!--   <property name="connection.driver_class">oracle.jdbc.xa.client.OracleXADataSource</property> -->
    <property name="connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property>
    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
    <property name="connection.username">scott</property>
    <property name="connection.password">tiger</property>
    <property name="show_sql">true</property>


    <property name="hibernate.connection.defaultAutoCommit">false</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
    <property name="format_sql">true</property>
    <property name="use_sql_comments">true</property>
<mapping resource="Employee.hbm.xml"/>
<mapping resource="LoginUser.hbm.xml"/>
<mapping resource="Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

客户.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
<class name="com.java.bean.Customer" table="Customer">
  <meta attribute="class-description">
     This class contains the Customer detail. 
  </meta>
  <id name="id" type="int" column="id">
     <generator class="native"/>
  </id>
  <property name="customerName" column="customerName" type="string"/>
  <property name="cifNumber" column="cifNumber" type="string"/>
  <property name="idNumber" column="idNumber" type="int"/>
  <property name="idCountry" column="idCountry" type="string"/>
  <property name="idType" column="idType" type="string"/>
  <property name="master_id" column="master_id" type="int"/>
  <property name="rmCode" column="rmCode" type="string"/>
  <property name="customerCountry" column="customerCountry" type="string"/>
 </class>

</hibernate-mapping>
4

1 回答 1

0

找到了根本原因,这是因为我的 id 列在映射文件中设置为主键,但在表中未设置为主键。

在我将 id 添加到主键后,此问题已得到解决。

于 2013-07-06T10:43:16.783 回答