为什么我的代码没有迭代 Hibernate 查询结果集?

发布于 2024-10-30 02:23:05 字数 2590 浏览 1 评论 0原文

我向 Hibernate 发出一个查询,它返回一个包含 25 个元素的列表。然后我迭代列表以打印出每个元素。问题在于它打印了第一个元素 25 次,而不是迭代这些元素。

这是第一个类 AnalAction 的代码:

package secondary.view;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.Session;
import secondary.controller.AnalManager;
import secondary.controller.LocationManager;
import secondary.model.Anal_01;
import secondary.model.LocationMaster;

import com.opensymphony.xwork2.ActionSupport;

public class AnalAction extends ActionSupport implements ServletRequestAware {
private List<LocationMaster> loclist;
    //private List<CustomerMaster> custList;
    //private ArrayList<String> bgroup;
    private ArrayList<String> Location;


public AnalAction()
    {
        linkcontroller=new AnalManager();
        linkcontroller2=new LocationManager();
        //custController=new CustomerMasterManager();

    }


String target="";
    public String execute()
    {
        try{
loclist=linkcontroller2.locList();  //calling from below class LocationManager
        System.out.println("loclist.."+loclist.size());
Location=new ArrayList<String>();
        Iterator<LocationMaster> iter = loclist.iterator();



 while(iter.hasNext())      
   {            
      String  str=iter.next().getLoc_name();
      System.out.println("location   name..:"+str);
      Location.add(str);
      target="Entry";
   }        
System.out.println("location..."+Location);

这是 LocationManager

package secondary.controller;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import secondary.model.LocationMaster;
import secondary.util.HibernateUtil;


public class LocationManager extends HibernateUtil {

public LocationManager(){};
@SuppressWarnings("unchecked")
public List<LocationMaster> locList()
{
    Session session=HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tran=null;
    List<LocationMaster> loc=null;
    try
    {
        tran=session.beginTransaction();
        loc=(List<LocationMaster>) session.createQuery("from LocationMaster    order by loc_code ").list();
        if(loc.size()>0)
        {
            System.out.println("size.....j"+loc.size());

            tran.commit();
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return loc;
}

}

I am firing a query off to Hibernate which returns a list of 25 elements. I then iterate over the list to print out each element. The problem is that it is printing out the first element 25 times, not iterating through the elements.

Here the code for the first class AnalAction:

package secondary.view;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.Session;
import secondary.controller.AnalManager;
import secondary.controller.LocationManager;
import secondary.model.Anal_01;
import secondary.model.LocationMaster;

import com.opensymphony.xwork2.ActionSupport;

public class AnalAction extends ActionSupport implements ServletRequestAware {
private List<LocationMaster> loclist;
    //private List<CustomerMaster> custList;
    //private ArrayList<String> bgroup;
    private ArrayList<String> Location;


public AnalAction()
    {
        linkcontroller=new AnalManager();
        linkcontroller2=new LocationManager();
        //custController=new CustomerMasterManager();

    }


String target="";
    public String execute()
    {
        try{
loclist=linkcontroller2.locList();  //calling from below class LocationManager
        System.out.println("loclist.."+loclist.size());
Location=new ArrayList<String>();
        Iterator<LocationMaster> iter = loclist.iterator();



 while(iter.hasNext())      
   {            
      String  str=iter.next().getLoc_name();
      System.out.println("location   name..:"+str);
      Location.add(str);
      target="Entry";
   }        
System.out.println("location..."+Location);

Here is LocationManager:

package secondary.controller;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import secondary.model.LocationMaster;
import secondary.util.HibernateUtil;


public class LocationManager extends HibernateUtil {

public LocationManager(){};
@SuppressWarnings("unchecked")
public List<LocationMaster> locList()
{
    Session session=HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tran=null;
    List<LocationMaster> loc=null;
    try
    {
        tran=session.beginTransaction();
        loc=(List<LocationMaster>) session.createQuery("from LocationMaster    order by loc_code ").list();
        if(loc.size()>0)
        {
            System.out.println("size.....j"+loc.size());

            tran.commit();
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return loc;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可可 2024-11-06 02:23:05

if(loc.size()>0) 上放置断点并检查 loc 以确保它包含您期望的内容,即 25 个不同的条目。

如果这与您期望的不符,则打开 SQL 日志记录以确保执行您期望执行的 SQL。

log4j.logger.org.hibernate.SQL=TRACE

Put a breakpoint on if(loc.size()>0) and inspect loc to ensure that it contains what you expect i.e. 25 different entries.

If this isn't as you expect then turn on SQL logging to ensure that the SQL you expect to be executed is.

log4j.logger.org.hibernate.SQL=TRACE
风柔一江水 2024-11-06 02:23:05

查看您的 LocationMaster 休眠映射。
loc 包含第一个元素 25 次,因为您的映射主键不是唯一的。

Take a look at your LocationMaster hibernate mapping.
loc contains 1st element 25 times because your mapped primary key is not unique.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文