首页 > 解决方案 > 无法使用作为映射值传递的 Java SQL 语句对象访问临时表

问题描述

我的代码目前的工作方式与此类似

public void foo (Statement st, String sqlStr, String tempTableName) {
String aString = foo(tempTableName);



boolean result = st.execute(aString); // creates temp table

// Now do something else in another method
writeToTT(st, tempTableName);

}

public void writeToTT(Statement st, String tempTableName) {
// Final String name
String finalSqlWithTempTableName = foo(tempTableName);

st.execute(finalSqlWithTempTableName);
}

以上工作正常。但是,当我将Statement对象作为 a 的一部分传递时,它在方法HashMap内部失败writeToTT

public void foo (Statement st, String sqlStr, String tempTableName) {
String aString = foo(tempTableName);

st.exectue(aString); // Creates temp table

// Now do something else in another method but pass it within a map
Map<String,Object> mapObj = new HashMap<>();
mapObj.put("statement",st);

writeToTT(mapObj, tempTableName);

}

public void writeToTT(Map<String,Object> mapObj, String tempTableName) {
// Final String name
String finalSqlWithTempTableName = foo(tempTableName);
Statement st = (Statement)mapObj.get("statement");
st.execute(finalSqlWithTempTableName); // Fails here saying that tempTableName object doesn't exist.

}

我使用调试器使用 System.identityHashCode(obj) 检查身份哈希码 - 两种情况(在方法框架堆栈内部和外部)哈希码都是相同的。它确实是使用传入的引用值引用同一个对象paramMap

我只是想了解这是因为它是如何作为新对象引用传递的,HashMap还是因为实际的语句对象没有有效的hashCode覆盖?

我的假设/理解是,即使对象在方法中保持不变,内存中的临时表位置和Statement对象对该内存位置的访问也会在我通过它时立即更改。但可能我弄错了吗?HashMaphashCodewriteToTT

标签: javasql-servertemp-tables

解决方案


推荐阅读