How to obtain the generated by database not id column after save

Viewed 182

The following entity has non id column COLUMN_NONID. This column is populated from database sequence called in trigger(before insert on table)

@Entity
@Table(name="tableABC")
public class ABCClass implements Serializable {

   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @Column(name="COLUMN_ID")
   private Long columnId;

   -----
   -----
   @Column(name="COLUMN_NONID")
   private Long nonIdColumn;
   ----
}

When I tried to insert new record in this table. The object abcClassEnity returned from save contain nonIdColumn value as null. This column value exists in database and it is defined as not null column. How I can retrieve this column value from save.

abcClassEnity = tableABCRepository.save(abcClassEnity);
1 Answers

The @Generated annotation was designed exactly for this purpose. You can correct your mapping in the following way:

@Generated(value = GenerationTime.INSERT)
@Column(name = "COLUMN_NONID", insertable = false)
private Long nonIdColumn;

See the hibernate documentation for the further explanation.

Related