簡體   English   中英

Spring Hibernate SQL 服務器 - 更新列時出現問題

[英]Spring Hibernate SQL Server - problem in updating columns

我收到以下錯誤,而根本沒有更新提到的列。 我只更新另外兩列,其中一列用於計算“可用”列。

無法修改“可用”列,因為它要么是計算列,要么是 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 配置如下:

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 服務器中的表定義如下:

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

當我添加@Generated 標簽時,我的問題得到了解決,如下所示:

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

但是我不明白為什么?!!! (因為當我使用本機查詢時,它似乎不是強制性的)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM