2013-07-04 2 views
0

Im пытается отобразить все строки в моей таблице, называемые клиентами, но она выполняет итерацию той же строки.Невозможно перечислить несколько строк через hibernate.

Например: у меня есть две строки с разными значениями, но первая строка печатает два раза. Просьба проконсультироваться.

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

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


} 

hibernate.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> 

Customer.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> 
+2

Пожалуйста, разместите свои файлы сопоставлений, а также используйте 'List cntctList = sql.list();' вместо того, чтобы кастинг на 'Object []' и повторное кастинг на 'Customer' –

+0

Привет, Raissi, я добавил отображение файлы, пожалуйста, помогите. – Vinoth

ответ

0

Найдено основной причиной, его благодаря тому, что мой столбец id задан как первичный ключ при отображении f iles, но в таблице он не задан как первичный ключ.

Это было разрешено после того, как я добавил идентификатор к первичному ключу.

Смежные вопросы