Skip to content
Advertisement

Spring Hibernate SQL Server – problem in updating columns

I get the below error while not updating the mentioned column at all. I only update two another columns that one of them is used to compute the column “Available”.

The column “Available” cannot be modified because it is either a computed column or is the result of a UNION operator.

I also used native query (as below) to be sure that there is no problem during translating hql to sql, but the problem still exists

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

Here is my model(table) definition:

@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;
}

My hibernate Configuration is as below:

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

Also my table definition in SQL Server is as below:

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

Advertisement

Answer

My problem was solved when I added @Generated tag as below:

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

But I can’t understand why ?!!! (because it does not seem to be mandatory specially when I use native query)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement