Java hibernate代码中的会话类型未定义session.connection()
当hibernate从hibernate 3.6升级到hibernate 4.1.2时,我面临session.connection()方法不被接受的问题。您能为替换session.connection方法提供一些解决方案吗 AuditLoginInterceptor.java:Java hibernate代码中的会话类型未定义session.connection(),java,spring,hibernate,session,Java,Spring,Hibernate,Session,当hibernate从hibernate 3.6升级到hibernate 4.1.2时,我面临session.connection()方法不被接受的问题。您能为替换session.connection方法提供一些解决方案吗 AuditLoginInterceptor.java: public class AuditLogInterceptor extends EmptyInterceptor{ Session session; private Set inserts = new
public class AuditLogInterceptor extends EmptyInterceptor{
Session session;
private Set inserts = new HashSet();
private Set updates = new HashSet();
private Set deletes = new HashSet();
public void setSession(Session session) {
this.session=session;
}
public boolean onSave(Object entity,Serializable id,
Object[] state,String[] propertyNames,Type[] types)
throws CallbackException {
System.out.println("onSave");
if (entity instanceof IAuditLog){
inserts.add(entity);
}
return false;
}
public boolean onFlushDirty(Object entity,Serializable id,
Object[] currentState,Object[] previousState,
String[] propertyNames,Type[] types)
throws CallbackException {
System.out.println("onFlushDirty");
if (entity instanceof IAuditLog){
updates.add(entity);
}
return false;
}
public void onDelete(Object entity, Serializable id,
Object[] state, String[] propertyNames,
Type[] types) {
System.out.println("onDelete");
if (entity instanceof IAuditLog){
deletes.add(entity);
}
}
//called before commit into database
public void preFlush(Iterator iterator) {
System.out.println("preFlush");
}
//called after committed into database
@SuppressWarnings("null")
public void postFlush(Iterator iterator) {
System.out.println("postFlush");
TbMasUsers tmu = null;
try{
for (Iterator it = inserts.iterator(); it.hasNext();) {
IAuditLog entity = (IAuditLog) it.next();
System.out.println("postFlush - insert");
try {
AuditLogUtil.LogIt("Saved",entity, session.connection());
// session.doWork(entity);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (Iterator it = updates.iterator(); it.hasNext();) {
IAuditLog entity = (IAuditLog) it.next();
System.out.println("postFlush - update");
try {
AuditLogUtil.LogIt("Updated",entity, session.connection());
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (Iterator it = deletes.iterator(); it.hasNext();) {
IAuditLog entity = (IAuditLog) it.next();
System.out.println("postFlush - delete");
try {
AuditLogUtil.LogIt("Deleted",entity, session.connection());
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} finally {
inserts.clear();
updates.clear();
deletes.clear();
}
}
}
AuditLogUtil.java:
session.connection()
在Hibernate 4中不推荐使用,您应该为此使用session.doWork()
API。在你的情况下,像这样的
s.doWork(new Work() {
@Override
public void execute(Connection c) throws SQLException {
AuditLogUtil.LogIt("Deleted",entity, c);
}
});
如果需要从execute()
方法内部返回值,请使用s.doReturningWork()
Map result=s.doReturningWork(新的ReturningWork(){
@凌驾
公共映射执行(连接c){
Map someMap=newhashmap();
返回someMap;
}
});
您可以在会话中使用doWork
getSession().doWork(new Work(){
@Override public void execute( Connection connection) throws SQLException {
}
}
);
或者使用
sessionFactory.withOptions()
修改会话以包含特定连接。正如您已经尝试过的,根据注释的代码判断,您应该使用session.doWork()
API。一个快速而肮脏的方法是将会话
强制转换为会话mpl
,其中connection()
方法可用,但已被弃用。如果您知道code会话,它在我尝试时不起作用。doWork()让我知道代码…谢谢,我已将其添加为答案,因此它可以很好地格式化
Map<Integer, String> result = s.doReturningWork(new ReturningWork<Map<Integer, String>>() {
@Override
public Map<Integer, String> execute(Connection c) {
Map<Integer, String> someMap = new HashMap<Integer, String>();
return someMap;
}
});
getSession().doWork(new Work(){
@Override public void execute( Connection connection) throws SQLException {
}
}
);