更新的实体未显示

时间:2015-07-28 18:06:39

标签: eclipse hibernate

这是我在hibernate中编写的程序,用于使用新线程更新名为emp的数据库中java3表中的三个字段。但是,更新后,它没有显示正确的记录。相反,它从缓存中获取数据。请指教。 session.refresh()未从数据库中再次加载实体。

package mypack;

import org.hibernate.*;

public class Refresher {

    public static void main(String[] args) {
        System.out.println("Main Thread Started...");
        Session session = MyFactory.getSession();
        System.out.println("Loading entity with id 1...");
        Emp e = (Emp) session.get(Emp.class, 1);
        System.out.println(e.getName() + "\t" + e.getJob() + "\t"
                + e.getSalary());
        System.out.println("Starting a new Thread...");
        Thread th = new Thread() {
            public void run() {
                System.out.println("New Thread Started...");
                System.out.println("Modifying entity with id 1...");
                Session session = MyFactory.getSession();
                Emp e = (Emp) session.get(Emp.class, 1);
                Transaction t = session.beginTransaction();
                e.setName("Kalpana Sharma");
                e.setJob("Yoga Instructor");
                e.setSalary(10000);
                t.commit();
                session.close();
                System.out.println("Entity updated, new thread completed...");
            }
        };
        th.start();
        System.out.println("Suspending main thread for 1 second...");
        try {
            Thread.sleep(10000);
        } catch (Exception ex) {
        }
        System.out.println("Main Thread resumed...");
        System.out.println("Refreshing the Entity...");
        //session.flush();
        //session.evict(e);
        session.refresh(e);
        System.out.println("Refreshed state of the entity...");
        System.out.println(e.getName() + "\t" + e.getJob() + "\t"
                + e.getSalary());
        session.close();
        System.out.println("Main Thread Completed.");
    }
}

该计划的输出:

Loading entity with id 1...
Hibernate: select emp0_.EmpId as EmpId1_0_0_, emp0_.name as name2_0_0_, emp0_.Designation as Designat3_0_0_, emp0_.salary as salary4_0_0_ from Emp emp0_ where emp0_.EmpId=?
Anoop Sharma    Software Engineer   35000
Starting a new Thread...
Suspending main thread for 1 second...
New Thread Started...
Modifying entity with id 1...
Hibernate: select emp0_.EmpId as EmpId1_0_0_, emp0_.name as name2_0_0_, emp0_.Designation as Designat3_0_0_, emp0_.salary as salary4_0_0_ from Emp emp0_ where emp0_.EmpId=?
Hibernate: update Emp set name=?, Designation=?, salary=? where EmpId=?
Entity updated, new thread completed...
Main Thread resumed...
Refreshing the Entity...
Hibernate: select emp0_.EmpId as EmpId1_0_0_, emp0_.name as name2_0_0_, emp0_.Designation as Designat3_0_0_, emp0_.salary as salary4_0_0_ from Emp emp0_ where emp0_.EmpId=?
Refreshed state of the entity...
Anoop Sharma    Software Engineer   35000
Main Thread Completed.

1 个答案:

答案 0 :(得分:1)

尝试更改beginTransactionget方法的序列,

...
 public void run() {
System.out.println("New Thread Started...");
                System.out.println("Modifying entity with id 1...");
                Session session = MyFactory.getSession();
                Transaction t = session.beginTransaction(); //first begin the transaction
                Emp e = (Emp) session.get(Emp.class, 1);                
                e.setName("Kalpana Sharma");
                e.setJob("Yoga Instructor");
                e.setSalary(10000);
                t.commit();
                session.close();
                System.out.println("Entity updated, new thread completed...");
            }
...