【问题标题】:Spring Hibernate SQL Server - problem in updating columnsSpring Hibernate SQL Server - 更新列的问题
【发布时间】:2023-03-24 03:47:01
【问题描述】:

我收到以下错误,但根本没有更新提到的列。我只更新另外两列,其中一列用于计算“可用”列。

无法修改“可用”列,因为它是计算列或 UNION 运算符的结果。

我也用了native query(如下)确保hql转sql时没有问题,但问题依旧存在

query = session.createQuery("update Retail.Account SET Balance = Balance + :Amount, RowVersion = RowVersion + 1 WHERE RowVersion = :RowVersion AND Id = :Id")

这是我的模型(表)定义:

@Entity 
@Table(name = "Account", schema = "Retail")
public class Account {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "Id")
public Integer Id;

// ..... some attributes

    
@Column(name = "Balance")
public BigDecimal Balance; // the column that I want to update

@Column(name = "Available")
public BigDecimal Available;// the computed column in my error

// ......

@Version
@Column(name = "RowVersion")
public Long RowVersion;
}

我的休眠配置如下:

hibernate.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
hibernate.url=########
hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
hibernate.username=**************
hibernate.password=**************
hibernate.hbm2ddl.auto=none
hibernate.setConnectionCachingEnabled=true
hibernate.show_sql=false
hibernate.format_sql=true

我在 SQL Server 中的表定义如下:

CREATE TABLE [Retail].[Account](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [CustomerId] [int] NOT NULL,
    [AccountTypeId] [int] NOT NULL,
    [OpeningDate] [datetime] NOT NULL,
    [StatusId] [int] NOT NULL,
    [Balance] [decimal](18, 2) NOT NULL,
    [Credit] [decimal](18, 2) NOT NULL,
    [Blocked] [decimal](18, 2) NULL,
    [Available]  AS (([Balance]+[Credit])-[Blocked]),
    [RowVersion] [bigint] NOT NULL,
 CONSTRAINT [PK_Account] PRIMARY KEY CLUSTERED 
([Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,     IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,  OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

【问题讨论】:

  • 请为我们提供声明
  • Hibernate 很可能会尝试更新列,即使您没有更改它。您需要提供更多信息,例如表的 Hibernate 配置、要更新的内容等。
  • edit 直接对问题进行任何澄清。并确保包含表定义。我假设您只是使用原始查询,而不是在 Hibernate 中配置表本身?
  • @H.Morshedlou 请同时显示您在 hql / jpql 中使用的实体
  • 仍然需要在数据库中查看您的表定义。

标签: java sql-server spring database hibernate


【解决方案1】:

当我添加@Generated 标记时,我的问题解决了,如下所示:

    @Generated( value = GenerationTime.ALWAYS )
    @Column(name = "Available")
    public BigDecimal Available;

但我不明白为什么?!!! (因为当我使用原生查询时,它似乎不是强制性的)

【讨论】:

    猜你喜欢
    • 2014-08-14
    • 1970-01-01
    • 2014-11-02
    • 2013-01-09
    • 2021-10-31
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    相关资源
    最近更新 更多