how to update record ignore id in mybatis and postgresql

varshadusa01
New Contributor

how to update record ignore id in mybatis and postgresql

Now I am using mybatis to update PostgreSQL 13 record, I want to using user id to update apple purchase prouct id in user table, this is my code:

Users users = new Users();
users.setId(payTransactionRecord.getUserId());
users.setAppleIapProductId(productId);

I give the user entity id and product id, update product id by using user id. this is the update execute code using mybatis:

public int updateByPrimaryKey(Users user) {
        UsersExample example = new UsersExample();
        UsersExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(user.getId());
        return userMapper.updateByExampleSelective(user,example);
    }

but the mybatis geneate the sql like this cause error:

Caused by: org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: org.postgresql.util.PSQLException: ERROR: column "id" can only be updated to DEFAULT
  Detail: Column "id" is an identity column defined as GENERATED ALWAYS.
### The error may exist in class path resource [mybatis/mapper/dolphin/UsersMapper.xml]
### The error may involve com.dolphin.soa.post.dao.UsersMapper.updateByExampleSelective-Inline
### The error occurred while setting parameters
### SQL: UPDATE users SET id = ?, apple_iap_product_id = ? WHERE (id = ?)
### Cause: org.postgresql.util.PSQLException: ERROR: column "id" can only be updated to DEFAULT
  Detail: Column "id" is an identity column defined as GENERATED ALWAYS.
; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: column "id" can only be updated to DEFAULT
  Detail: Column "id" is an identity column defined as GENERATED ALWAYS.

what should I do to fix it?

1 REPLY 1
MarkWarren
Expert

Re: how to update record ignore id in mybatis and postgresql

I think you may have the wrong forum, this is for Anaplan!
This post does not relate to Anaplan.

however, I think your problem lies in that your table has the ID field defined as GENERATED ALWAYS, you should have it set to GENERATED BY DEFAULT if you want to set a specified value in your line: 

users.setId(payTransactionRecord.getUserId());