我使用休眠创建了一个简单的 hello-wordish 代码
sf = new Configuration().configure().buildSessionFactory();
System.out.println("sessionFactory is built.Get session object");
s = sf.openSession();
Kk emp = (Kk) s.get(Kk.class, 1);
if (null != emp) {
System.out.println(emp.getId() + " " + emp.getNm());
} else {
System.out.println("Emp doesn't exist..");
}
这段代码运行良好,并给了我 1 条记录,如下面的控制台所示:-
sessionFactory is built.Get session object
Hibernate: select kk0_.id as id0_0_, kk0_.name as name0_0_ from kaushik1 kk0_ where kk0_.id=?
1 kaushik
但是当我通过 hibernate 配置引入一个 Load 事件监听器类时,并没有获取到记录。而是查询本身不会被触发。
我的听众课是:-
import org.hibernate.HibernateException;
import org.hibernate.event.LoadEvent;
import org.hibernate.event.LoadEventListener;
public class MyLoadListener implements LoadEventListener {
public void onLoad(LoadEvent arg0, LoadType arg1) throws HibernateException {
System.out.println("inside my own onLoad listener");
}
}
在我的 hibernate.cfg.xml 中,我添加了一行:-
<listener type="load" class="com.cts.eventlistener.MyLoadListener"/>
现在,当调用相同的代码时,我可以在控制台上看到:-
sessionFactory is built.Get session object
inside my own onLoad listener
Emp doesn't exist..
为什么调用事件侦听器的 onLoad 时没有调用查询?