java - 无法使用作为映射值传递的 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
对象对该内存位置的访问也会在我通过它时立即更改。但可能我弄错了吗?HashMap
hashCode
writeToTT
解决方案
推荐阅读
- azure - 使用 WSO2 AM 配置 Azure AD 安全 LDAP
- c# - EF 动态投影未通过最小起订量测试
- kettle - 如何在查询中转义 PDI Pentaho 中的问号 (?) 字符
- php - 如何从托管服务 1&1 中查找信息以便使用 PHP 邮件程序发送电子邮件?
- dart - Flutter:“NoSuchMethodError”不是字符串类型的子类型
- javascript - 没有实际解析的美化器?
- c# - 为什么“out”参数作为语言结构存在于 C# 中?
- spring-boot - Springboot & Thymeleaf HTML模板:转PDF后汉字消失
- javascript - window.parent.postMessage() 在一堆重定向后不起作用
- python - Python类变量不保持价值