Java Spring Hibernate SQL Server-更新列时出现问题

Java Spring Hibernate SQL Server-更新列时出现问题,java,sql-server,spring,database,hibernate,Java,Sql Server,Spring,Database,Hibernate,我在根本不更新所提到的列的情况下出现以下错误。我只更新了另外两列,其中一列用于计算列“Available” 无法修改列“Available”,因为它是计算列或是UNION运算符的结果 我还使用了本机查询(如下所示),以确保在将hql转换为sql的过程中不会出现问题,但问题仍然存在 query = session.createQuery("update Retail.Account SET Balance = Balance + :Amount, RowVersion = RowVers

我在根本不更新所提到的列的情况下出现以下错误。我只更新了另外两列,其中一列用于计算列“Available”

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

我还使用了本机查询(如下所示),以确保在将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
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( value = GenerationTime.ALWAYS )
    @Column(name = "Available")
    public BigDecimal Available;
我在SQL Server中的表定义如下:

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
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( value = GenerationTime.ALWAYS )
    @Column(name = "Available")
    public BigDecimal Available;

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

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
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( value = GenerationTime.ALWAYS )
    @Column(name = "Available")
    public BigDecimal Available;

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

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