尝试检索插入的行时插入失败

时间:2011-08-11 10:45:39

标签: hibernate

我有一个实体,其id是SQL Server数据库中生成的GUID(它是我无法更改模式的第三方数据库),因此定义了字段;

@Id
@GenericGenerator(name="generator", strategy="guid", parameters = {})
@GeneratedValue(generator ="generator")
@Column(name="FarmID")
public String getId() {
    return id;
}

当我创建一个新实体并保存它时,首先由GUIDGenerator调用 newid()正确生成id,然后使用生成的id作为参数值插入行。但是,插入后Hibernate尝试检索新插入的行但是失败并且结果是它使用了空白的id值 - 这是跟踪日志;

[main]  AbstractBatcher                DEBUG about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[main]  SQL                            DEBUG 
select
    newid()
[main]  AbstractBatcher                TRACE preparing statement
[main]  GUIDGenerator                  DEBUG GUID identifier generated: 5B5495B4-FFE3-4112-B079-4FB799320BA7
[main]  AbstractBatcher                DEBUG about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[main]  AbstractBatcher                TRACE closing statement
[main]  ConnectionManager              TRACE registering flush begin
[main]  AbstractBatcher                DEBUG about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[main]  SQL                            DEBUG 
insert 
into
    Farm
    (FarmBusinessID, FarmName, PortalFarm, FarmID) 
values
    (?, ?, ?, ?)
[main]  AbstractBatcher                TRACE preparing statement
[main]  BasicBinder                    TRACE binding parameter [1] as [VARCHAR] - F2542B84-82E4-4DF6-BDBB-E830BFE5DDC1
[main]  BasicBinder                    TRACE binding parameter [2] as [VARCHAR] - XXXXXX XXXXX (XXX)
[main]  BasicBinder                    TRACE binding parameter [3] as [BIT] - true
[main]  BasicBinder                    TRACE binding parameter [4] as [VARCHAR] - 5B5495B4-FFE3-4112-B079-4FB799320BA7
[main]  AbstractBatcher                DEBUG about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[main]  AbstractBatcher                TRACE closing statement
[main]  ConnectionManager              TRACE registering flush end
[main]  HQLQueryPlan                   TRACE find: select id from agronomy.model.hub.Farm where id = :id
[main]  AbstractBatcher                DEBUG about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[main]  SQL                            DEBUG 
select
    farm0_.FarmID as col_0_0_ 
from
    Farm farm0_ 
where
    farm0_.FarmID=?
[main]  AbstractBatcher                TRACE preparing statement
[main]  BasicBinder                    TRACE binding parameter [1] as [VARCHAR] - 
[main]  AbstractBatcher                DEBUG about to open ResultSet (open ResultSets: 0, globally: 0)
[main]  AbstractBatcher                DEBUG about to close ResultSet (open ResultSets: 1, globally: 1)
[main]  AbstractBatcher                DEBUG about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[main]  AbstractBatcher                TRACE closing statement
[main]  JDBCExceptionReporter          WARN  SQL Error: 8169, SQLState: S0002
[main]  JDBCExceptionReporter          ERROR Conversion failed when converting from a character string to uniqueidentifier.

我不知道我是在显式调用最后一个SELECT(失败),所以我不确定在哪里可以看到它失败的原因。 SQL错误大致翻译为“你不能使用空字符串作为uniqueidentifier值”,这很有意义。

1 个答案:

答案 0 :(得分:0)

我永远无法理解这一点,但幸运的是,我错误地认为这是第三方数据库,它给了我这个问题,而事实上架构是在我们的控制之下。 / p>

只需将表上的主键更改为BIGINT(设置为标识列)并将实体ID的数据类型更改为Long,我就可以重新运行测试代码并将实体正确地保存到数据库中。一切都按预期工作,我可以停止撕裂我的聆听和咒骂。

为什么GUID不起作用的底部会很好,但在这种情况下,生命太短暂,替代方案是可以接受的。祝所有遇到类似问题的人好运。