Java、SQL Server、PreparedStatementを使って発行したset IDENTITY_INSERTが反映されない
タイトルどおり。ORマッパ経由でクエリ投げてたので内部で何してるかわからず、原因調査に手間取って死だった。
Connection con=getConnection(); // リソース開放してないのは多めに見てほしい con.prepareStatement("set IDENTITY_INSERT hoge ON").execute(); con.prepareStatement("insert into hoge(identity_value) values(1)").exceute(); // 例外: com.microsoft.sqlserver.jdbc.SQLServerException: IDENTITY_INSERT が OFF に設定されているときは、テーブル 'hoge' の ID 列に明示的な値を挿入できません。 con.createStatement().execute("set IDENTITY_INSERT hoge ON"); con.prepareStatement("insert into hoge(identity_value) values(1)").exceute(); // 成功