Some preconditions:
I am not using Oracle DB sequence generator. Instead of it, I rely on the Hibernate sequence generator ex
@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.
You can't; the act of persisting itself is what creates the ID.
In your case, which uses a UUID generator, I think you can use the Lifecycle interface and implement the onSave
method returning NO_VETO
.
eg
@Entity
public MyEntity implements Lifecycle {
@Id
@GeneratedValue
private UUID id;
...
public boolean onSave(final Session session) throws CallbackException {
// here your entity will have an ID
return Lifecycle.NO_VETO;
}
...
}
The onSave
method will be called before saving the entity and after generating the ID.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.