我有以下代码,插件正在完成,但查询()中的查询不会打印它所假设的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
答案 0 :(得分:0)
query()
方法未执行,因为您没有调用它。
无论如何,除非你main()
static
,否则你无法从public static void query()
拨打电话:
main()
运行类时只会执行static
方法,因此在将query()
添加到main()
的方法签名后,将此行添加到{{1方法:
query();
答案 1 :(得分:0)
如果您正在调用query()
方法并且它仍然没有打印任何内容,则表示您的emp
列表为空。如果您有任何符合查询限制的记录,请检查您的数据库。如果您这样做,请检查列表emp
的内容。