gpt4 book ai didi

Hibernate 3.5 与 4 IDENTITY_INSERT 问题

转载 作者:行者123 更新时间:2023-12-02 22:25:25 24 4
gpt4 key购买 nike

我们运行 Spring 3.1/Hibernate 4/Java 7/Tomcat 7/MSSQL 2008 R2 Web 应用程序。我们必须处理遗留数据和存档数据。从存档中提取数据时,我们需要使用原始的唯一标识符,以便其他(非存档)记录能够正确重新水合。这些标识符存储在主键/自动增量字段中。

在此之前,当我们使用 Spring 3.0/Hibernate 3.5 时,以下代码用于将提取的记录插入到相应的表中(我们已经在其中包含变量 sessionentityfullTableName )范围):

session.doWork( new Work() 
{
@Override
public void execute(Connection connection) throws SQLException
{
PreparedStatement statement = null;
try
{
statement = connection.prepareStatement(String.format("SET IDENTITY_INSERT %s ON", fullTableName));
statement.execute();

session.save(entity);

statement = connection.prepareStatement(String.format("SET IDENTITY_INSERT %s OFF", fullTableName));
statement.execute();
}
finally
{ /* close the statement */ }
}
});

就像我提到的,这一切在 Hibernate 3.5 中都工作得很好,但现在我们已经升级到 Hibernate 4,它已经停止工作了。 Work 和isolatedwork 之间有什么区别吗?

为了解决问题并避免任何工作界面问题,我们尝试了以下操作:

session.createSQLQuery(String.format("SET IDENTITY_INSERT %s ON", fullTableName)).executeUpdate();
session.save(entity);
session.createSQLQuery(String.format("SET IDENTITY_INSERT %s OFF", fullTableName)).executeUpdate();

但是,这也不起作用。具体来说,抛出的异常是 java.sql.SQLException: Cannot insert explicit value for identity column in table 'Employee' when IDENTITY_INSERT is set to OFF.然而,应该清楚的是,我们正在努力将其设置为ON。

我们对情况进行了 SQL Server Profiler 跟踪,并发现了一些有趣的事情。在我们的每个事务主体中都设置了 IMPLICIT_TRANSACTIONS ON。以下是 Profiler 跟踪的一些示例输出(我已将实际架构替换为 <schema> ,以及一些带有较短标签的大量数据):

SET IMPLICIT_TRANSACTIONS ON
go
declare @p1 int
set @p1=55
exec sp_prepare @p1 output,N'',N'SET IDENTITY_INSERT <schema>.Employee ON',1
select @p1
go
exec sp_execute 55
go

declare @p1 int
set @p1=56
exec sp_prepare @p1 output,N'<parameters for the INSERT>',N'insert into <schema>.Employee (<all the column names>) values ( <all the parameters> )',1
select @p1
go
exec sp_execute 56,<the actual values to insert>
go
IF @@TRANCOUNT > 0 ROLLBACK TRAN
go
IF @@TRANCOUNT > 0 COMMIT TRAN
SET IMPLICIT_TRANSACTIONS OFF
go
exec sp_execute 54,N'Error writing EMPLOYEE archive record. ',<an id>,N'1'
go

现在,我们在事务中通过 Connection.setAutoCommit(false) 特别将 IMPLICIT_TRANSACTIONS 设置为 OFF(事务通过 Spring @Transactional 和 Hibernate Transaction Manager 进行管理)。显然,这是行不通的,但是除了使用 setAutoCommit 之外还有什么其他选择,为什么它可以在 Spring3.0/Hibernate 3.5 中工作,但不能在 Spring 3.1/Hibernate 4 中工作?

感谢您的任何想法或建议 - 我们很难过。

最佳答案

嗯,这是一个微妙的解决方案......

我们的 Work 调用在内部使用了 java.sql.PreparedStatement,然后调用了 execute() 方法。显然,这告诉 SQL Server 将命令包装在它自己的存储过程中,如一些代码示例所示。

我们从使用 PreparedStatement 更改为简单的 java.sql.Statement 并调用其 execute() 方法:

session.doWork( new Work() 
{
@Override
public void execute(Connection connection) throws SQLException
{
Statement statement = null;
try
{
statement = connection.createStatement();
statement.execute(String.format("SET IDENTITY_INSERT %s ON", fullTableName));

session.save(entity);

statement = connection.createStatement();
statement.execute(String.format("SET IDENTITY_INSERT %s OFF", fullTableName));
}
finally
{ /* close the statement */ }
}
});

那么,有什么区别呢?据我们所知,PreparedStatement 生成预编译的 SQL,而 Statement 生成静态 SQL...正是我们调用 IDENTITY_INSERT 所需的内容!

教训:败类和恶行层出不穷……我们必须小心谨慎!

关于Hibernate 3.5 与 4 IDENTITY_INSERT 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12921560/

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