Generate id using dbsequence in Oracle ADF Without Losing it .

Requirements: Populate Primary key or ID using dbsequence in Oracle ADF only during database commit

Solution: In my previous post of generating the sequence generating sequence in ADF ,previous post ,But the problem is you will lose sequence at time of error or cancel time.

Following changes to achieve this requirement

1. Go the entity and put a default value as some -999 or -9999 (This cant be your dbsequence value, preferably negative values)

2. Generate EntityImpl.class file and add below lines of code to the file at the end.

Note: Here we are overriding doDML() method of EntityImpl.class so that it will the get the value from db sequence just before commit to database.

    public void doDML(int operation, TransactionEvent e) {
        this.setEmployeeId((Integer)(new SequenceImpl("Vinaytableid_seq",getDBTransaction()).getSequenceNumber()).intValue());
        super.doDML(operation, e);

Note2: it is not a mandatory rule to modify doDML() method only, you can use prepareForDML() method as well if you like. As doDML() is the last method that gets fired before beforeCommit() method, I preferred it.

3. In UI page, users don’t want to see any negative values initially, to show it as blank, just put an ELExpression to hide it whenever the value is negative.i.e
#{bindings.ContractId.inputValue == -1 ? ” : bindings.EmployeeId.inputValue}

Happy coding with techartifact