问题是,一旦通过登录启动休眠模式,我将无法完全终止该程序,在Eclipse中,我必须在关闭程序后手动使用终止选项,并且作为可执行jar,该程序仍会作为Java进程保持打开状态关闭时。
这是登录类使用的方法:
public class LoginService {
private User user = null;
// email = username
public User getUser(String email) throws Exception {
List<User> userList = new ArrayList<>();
Session session = ConnectionHelper.getSessionFactory().openSession(); // Creates session
CriteriaBuilder builder = session.getCriteriaBuilder(); // Create builder
CriteriaQuery<User> query = builder.createQuery(User.class); // Create query
Root<User> userRoot = query.from(User.class); // SELECT FROM USER
query.where(builder.equal(userRoot.get("email"), email)); // WHERE EMAIL = ????
userList = session.createQuery(query).getResultList(); // Runs the query, gets result
if (userList.size()>0) { // Checks for null value
user = userList.get(0); // Gets value if not null
}
return user; // Returns user object
}
}
并且此类具有连接信息:
public class ConnectionHelper {
private static SessionFactory sessionFactory = null;
private static ServiceRegistry registry;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
createSessionFactory();
}
return sessionFactory;
}
private synchronized static void createSessionFactory() {
Configuration config = new Configuration().configure("/res/hibernate.cfg.xml");
config.addAnnotatedClass(User.class);
registry = new StandardServiceRegistryBuilder().applySettings(
config.getProperties()).build();
sessionFactory = config.buildSessionFactory(registry);
}
public void stop() throws Exception {
sessionFactory.close();
}
}
我曾尝试在登录服务方法中使用session.close,其中对用户/密码进行了身份验证,但这没有用。