[JPA_SPEC-133] Make TABLE-generated IDs available on PrePersist Created: 09/Aug/16  Updated: 22/Aug/16

Status: Open
Project: jpa-spec
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor
Reporter: jinahya Assignee: ldemichiel
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Tags: generator, id, lifecycle


JSR-338/3.5.3 states that

> Generated primary key values are available in the PostPersist method.

It'll be much helpful if those ids whose strategy is GenerationType.TABLE are available on PrePersist.

exempli gratia

public class MyEntity {

    private void onPrePersist() {
        // generate a unique and unchangable value from *available* id.
        derived = String.format("%1$016x", id);

    @GeneratedValue(..., strategy = GenerationType.TABLE)
    private Long id;

    @Basic(optional = false)
    @Colum(..., nullable = false, updatable = false)
    private String derived;

Note that the derived column is not null and not updatable.

Comment by neilstockton [ 22/Aug/16 ]

FWIW DataNucleus JPA already does set ids from all non-IDENTITY value generators (i.e TABLE, SEQUENCE, custom) prior to PrePersist, so hence the values are available in that method.

Generated at Wed Aug 24 20:48:35 UTC 2016 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.