我有以下代码并且正在插入,但是 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