0

我有以下代码并且正在插入,但是 query() 中的查询没有打印它假设的 println。

package com;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import roseindia.tutorial.hibernate.Insurance;

import com.Employee;

public class EmployeeEx 
{

public static void main(String[] args) 
{
    Session session = null;

    try
    {       
        Transaction transaction = null; 

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        session =sessionFactory.openSession();

        File f=new File("c:/Class/Employee1.txt") ;
        Scanner scan=new Scanner(f);

        transaction = session.beginTransaction();
        while(scan.hasNext())
        {               
            Employee em = new Employee();
            String line=scan.nextLine();            
            String empArray[]=line.split(" ");

            em.setId(Integer.parseInt(empArray[0]));
            em.setName(empArray[1]);
            em.setSalary(Double.parseDouble(empArray[2]));
            em.setManager(empArray[3]);
            session.save(em);
            transaction.commit();
        }   
  query();//calling the query method

}
catch(Exception e)
{
    System.out.println(e.getMessage());
}

finally
{
    session.flush();
    session.close();
}
}

public static void query()
{
    Session session = null;
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    session =sessionFactory.openSession();

    Criteria criteria = session.createCriteria(Employee.class);
    criteria.add(Restrictions.like("manager", "Anurag"));
    criteria.add(Restrictions.gt("salary", 1000));

    List emp = criteria.list();
    for(Iterator it = emp.iterator();it.hasNext();)
    {
        Employee em = (Employee) it.next();
        System.out.println("Id: " +em.getId());;
        System.out.println("Name: " +em.getName());
        System.out.println("Salary: " +em.getSalary());
        System.out.println("Manager: " +em.getManager());

    }
    session.close();
}

}

这是控制台上显示的内容:(插入工作正常)

Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?)
Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?)
Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?)
Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?)
Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?)
Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?)
Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?)
Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?)
Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?)
Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?)
Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?)
Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?)
Hibernate: select this_.ID as ID0_, this_.NAME as NAME7_0_, this_.SALARY as SALARY7_0_,    this_.MANAGER as MANAGER7_0_ from EMPLOYEE1 this_ where this_.MANAGER like ? and this_.SALARY>? java.lang.Integer
4

2 回答 2

0

query()方法没有被执行,因为您没有调用它。

main()无论如何,除非您成功,否则您无法调用它static

public static void query()

当你运行一个类时,只有main()方法会被执行,所以在你添加static到方法签名之后query(),将这一行添加到你的main()方法中:

query();
于 2012-08-17T18:27:58.180 回答
0

如果您正在调用该query()方法并且它仍然没有打印任何内容,那么这意味着您的emp列表是空的。如果您有任何符合查询限制的记录,请检查您的数据库。如果这样做,请检查 list 的内容emp

于 2012-08-20T00:49:23.190 回答