gpt4 book ai didi

java - 在 HIbernate/SQL 中获取 Session 和 Release session 的最佳方式

转载 作者:行者123 更新时间:2023-11-29 08:07:47 24 4
gpt4 key购买 nike

考虑以下两段代码。

Session session = null;
query = "update Employee set EMPLOYEE_NAME = 'Jay' where EMPLOYEE_ID = 1";

try {
session = getSession();
Query query = session.createSQLQuery(dlquery);
query.executeUpdate();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if(session != null) {
releaseSession(session);
}
}

还有....

Session session = getSession();
query = "update Employee set EMPLOYEE_NAME = 'Jay' where EMPLOYEE_ID = 1";
try {
Query query = session.createSQLQuery(dlquery);
query.executeUpdate();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if(session != null) {
releaseSession(session);
}
}

两者哪个更好用?或者,还有更好的方法?我在这里可能做错了什么?

最后我们是否应该在调用 releaseSession(session) 之前做一个 null 检查?

最佳答案

确实,最好的方法是利用 Spring 及其 JPA/Hibernate 支持......您永远不必在代码中处理它。

关于java - 在 HIbernate/SQL 中获取 Session 和 Release session 的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9943879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com