0

Thanks to read this question. I'm using Spring Data JPA + PostgreSql.

I referred the url > Hibernate JPA Sequence (non-Id) to set the auto incremental property in non-id(pk) column but it didn't work (inserted null value).

Here is the part of @Entity

@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
String id;

@Column(name = "agent_no", columnDefinition = "serial", insertable = false, updatable = false)
@Generated(GenerationTime.INSERT)
private Integer agentNo;

Here is the part of @Service

/* POST */
@Transactional
public  AgentDto create(AgentUpsertDto newItem) throws Exception {
    Optional.ofNullable(newItem).orElseThrow(() -> new DataException("No Data"));
    UEMspAgent item = newItem.createBy(modelMapper);
    item = agentRepository.save(item);
    return modelMapper.map(item, AgentDto.class);
}

Here is the part of AgentUpsertDto

private String id;
private Integer agentNo;
private String displayName;
private String ipAddress;

@Override
public Agent createBy(ModelMapper mapper) throws Exception {
    // TODO Auto-generated method stub
    Agent entity = mapper.map(this, Agent.class);
    
    /* */
    if (StringUtils.isAllEmpty(entity.getDisplayName())) {
        throw new DataException("No Data");
    }
    if (StringUtils.isAllEmpty(entity.getIpAddress())) {
        throw new DataException("No Data");
    }
    
    return entity;
}

I expected to column 'agentNo' had auto incremental property, but when the data inserted the column was null (and others are okay). Would you help me to solve this problem?

Tom
  • 1
  • 1

0 Answers0