Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get generated entity Id before persisting, Hibernate, Spring data

Some preconditions:

I am not using Oracle DB sequence generator. Instead of it, I rely on the Hibernate sequence generator e.x.

@Entity
@Table(name = "JPA_ENTITY_A")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
public class JpaEntityA{
    @Id
    @Type(type = "uuid-binary")
    @GeneratedValue(generator = "system-uuid")
    private UUID id;
    @Column(name="NAME_WITH_ID")
    String nameWithGeneratedId;
}

What I want is to persist the following generated value into the column "NAME_WITH_ID": this.nameWithGeneratedId+this.id

Is it feasible to do the following:

public String getNameWithGeneratedId(){
    return this.nameWithGeneratedId+this.id;//hope that the returned value will be persisted
}

Or is it possible to retrieve in advance before persisting entity to the DB generated id? If yes, then how can I accomplish it? (based on the comments below it is not possible to do it)

Thx in advance.

like image 809
mr.M Avatar asked Nov 09 '22 09:11

mr.M


1 Answers

You can't; the act of persisting itself is what creates the ID.

like image 183
chrylis -cautiouslyoptimistic- Avatar answered Nov 14 '22 22:11

chrylis -cautiouslyoptimistic-