gpt4 book ai didi

java - hibernate 代码中的 session.connection() 类型未定义

转载 作者:行者123 更新时间:2023-12-02 07:52:26 28 4
gpt4 key购买 nike

当我从 hibernate 3.6 升级到 hibernate 4.1.2 时,我遇到了 session.connection() 方法不被接受的问题。您能提供一些替换 session.connection 方法的解决方案吗?

AuditLogInterceptor.java:

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:

public class AuditLogUtil{


public static void LogIt(String action,
IAuditLog entity, Connection conn) throws Exception{
SessionServiceImpl sessionServiceImpl=new SessionServiceImpl();
TbMasUsers tbMasUsers=(TbMasUsers) sessionServiceImpl.getUserInSession();
Integer loingEmpId=Integer.parseInt(tbMasUsers.getIntEmpId().getStrEmpId());

//SessionImpl sessionImpl = (SessionImpl) session;
// Connection conn1 = sessionImpl.connection();
Session tempSession = HibernateUtil.getSessionFactory().openSession(conn);


try {
@SuppressWarnings("null")
AuditLog auditRecord = new AuditLog(action,entity.getLogDeatil()
, new Date(),entity.getId(), entity.getClass().toString(),loingEmpId);
tempSession.save(auditRecord);
tempSession.flush();

} finally {
tempSession.close();

}

}
}

最佳答案

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<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;
}
});

关于java - hibernate 代码中的 session.connection() 类型未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32479459/

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