Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

6/30/2020 Using Groovy Expression to set a Primary Key with a Sequence Number | JDev & ADF Goodies

JDev & ADF Goodies


Tips & Tricks for JDeveloper and ADF

Using Groovy Expression to set a Primary Key


with a Sequence Number
Posted on 2. September 2011

Just set up a workspace for JDeveloper 11.1.2.0.0 to show how to use a Groovy expression to set the primary
key of an entity object. Chris Muir blogged about this back in 2009 here ADF BC: Using Groovy to fetch
sequence numbers for EO/VO attribute default values. There exists a white paper Introduction to Groovy Support
in JDeveloper and Oracle ADF 11g which covers Groovy support in JDeveloper.
So, I don’t cover the basic here but only show how to use this in a sample application. The application uses the
HR schema and allows you to insert a new employee. The workspace which you can download (see at the end of
this blog) is build using JDeveloper 11.1.2.0.0.
I use the EMPLOYEES_SEQ defined in the HR schema to set the PK of the new employee, EMPLOYEE_ID to
the next available sequence number.

— Employee Sequence

Now weThis
Privacy & Cookies: cansite
open thecookies.
uses Employee EO from to
By continuing the model
use layer. Double
this website, click
you agree to on
theirthe Employees EO to open the
use.
To find out properties inspector
more, including how tofor the EO
control and select
cookies, theCookie
see here: ‘Attribute’ section.
Policy

Close and accept

https://tompeez.wordpress.com/2011/09/02/using-groovy-expression-to-set-a-primary-key-with-a-sequence-number/ 1/6
6/30/2020 Using Groovy Expression to set a Primary Key with a Sequence Number | JDev & ADF Goodies

— Employees Attributes

Now select the EmployeeId in the attributes to get to the attributes properties.

— EmployeeId Properties

Here we can add a default value as literal, expression or as SQL. We select the ‘Expression’ radio button and
click on hte pencel on the right side of the input field.

— Edit Expression Editor

Here we enter the Groovy expression to get the next sequence number

1 (new oracle.jbo.server.SequenceImpl("EMPLOYEES_SEQ",adf.object.getDBTransaction())).ge

After submitting the dialog with OK you should set the ‘Refresh Expression Value’ to ‘true’ and the ‘Updatable’
LOV to ‘While New’.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Close and accept

https://tompeez.wordpress.com/2011/09/02/using-groovy-expression-to-set-a-primary-key-with-a-sequence-number/ 2/6
6/30/2020 Using Groovy Expression to set a Primary Key with a Sequence Number | JDev & ADF Goodies

— Finished Dialog

A word of caution here: JDev 11.1.2.0.0 saves the setting for the refresh condition in the xml file, but the next time
you open the dialog again the ‘Refresh Expression Value’ value is gone! I’ll file a bug for this later.
This wraps up the the model layer of the app. You can test your work with the Application Module Tester. When
you create a new record you’ll see that the EmployeeId is set to the next sequence number.

— Oracle ADF Model Tester

The ViewController project is pretty simple. It consists of an ADF form with with navigation buttons and a button
to call a bounded task flow to create the new employee.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Close and accept


— Task Flows

https://tompeez.wordpress.com/2011/09/02/using-groovy-expression-to-set-a-primary-key-with-a-sequence-number/ 3/6
6/30/2020 Using Groovy Expression to set a Primary Key with a Sequence Number | JDev & ADF Goodies

The bounded task flow first calls the CreateInsert operation to create a new record which is then displayed in the
form. As you notice, the EmployeeId is an af:outputText element, so that you can’t change it.

— Running Application

The workspace for JDeveloper 11.1.2.0.0 can be downloaded from here: Workspace BlogPKwithGrooy.zip
After downloading the file, remove the suffix ‘.doc’ and rename it to ‘ BlogPKwithGrooy.zip’, as the file is a Zip file.

TEIL E N S I E DIES MIT:

 Twitter

Like

Be the first to like this.

RELAT E D

JDeveloper Versions vs. JDeveloper: Fitler Table on Using one ViewObject for
Weblogic Server Versions Transient Column Global Lookup Data
In "ADF" In "ADF" In "ADF Faces"

This entry was posted in ADF, ADF Faces, ADF Faces RC, JDEV11.1.2.0.0, JDeveloper, Uncategorized and
tagged 11.1.2.0.0, ADF, adf faces, ADF Faces RC, entity objects, Groovy, JDEV11.1.2.0.0, JDeveloper,
sample, Sequence by Timo Hahn. Bookmark the permalink
[https://tompeez.wordpress.com/2011/09/02/using-groovy-expression-to-set-a-primary-key-with-a-
sequence-number/] .

7 THOUGHTS ON “USING GROOVY EXPRESSION TO SET A PRIMARY KEY WITH A SEQUENCE NUMBER”

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Pratik Soni
Close and accept
on 27. February 2012 at 14:14 said:

https://tompeez.wordpress.com/2011/09/02/using-groovy-expression-to-set-a-primary-key-with-a-sequence-number/ 4/6
6/30/2020 Using Groovy Expression to set a Primary Key with a Sequence Number | JDev & ADF Goodies

This worked for me thanks..

Hemanshu
on 26. March 2012 at 20:20 said:

Hi,
I am getting the following error:
<ADF: Adding the following JSF error message: startup failed,
bc4j.net_scotwat_metering_vacancy_createsurveybatch_model_entityobj_SPIDbatchRefEO_SpidBatchRefSk_null.gs.groovy
1: expecting anything but ''\n''; got it anyway @ line 1, column
81.org.codehaus.groovy.syntax.SyntaxException: expecting anything but ''\n''; got it anyway @ line 1,
column 81.

I have given the groovy exp as (new


oracle.jbo.server.SequenceImpl("MTRNG_SPID_BATCH_REF_SEQ",adf.object.getDBTransaction())).getSequenceNumber(

and on my AMImpl I have the following code

ViewObjectImpl spidbatchvo = getSPIDbatchRefVO1();


ViewObjectImpl spidresultvo = getSearchSPIDVO1();
Iterator itr = spidlkup.iterator();
Iterator itr1=priority.iterator();
while(itr.hasNext() && itr1.hasNext()){
BigDecimal spid = (BigDecimal)itr.next();
String Prio=(String)itr1.next();
Row r = spidbatchvo.createRow();
r.setAttribute("SpidLkupSk", spid);
r.setAttribute("VacSurveyBatchSk", batchId);
r.setAttribute("CreatedBy", userName);//user name
r.setAttribute("CreatedDt", ((DBTransactionImpl)getDBTransaction()).getCurrentDbTime());
// r.setAttribute("UpdatedBy", "TestUser");
// r.setAttribute("UpdatedDt", ((DBTransactionImpl)getDBTransaction()).getCurrentDbTime());
r.setAttribute("DeleteFlag", 1);
r.setAttribute("Priority",Prio);
spidbatchvo.insertRow(r);
}
this.getDBTransaction().commit();

I am getting the above mentioned exception at the line:


Row r = spidbatchvo.createRow();

Any pointers ?

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control
Timo Hahn cookies, see here: Cookie Policy
on 27. March 2012 at 22:22 said:
Close and accept

https://tompeez.wordpress.com/2011/09/02/using-groovy-expression-to-set-a-primary-key-with-a-sequence-number/ 5/6
6/30/2020 Using Groovy Expression to set a Primary Key with a Sequence Number | JDev & ADF Goodies

Sorry, never seen this. You better open a thread in on the OTN forum and give all details about
your environment in the thread.

Timo

unknownsoul
on 11. July 2013 at 08:08 said:

Hi, for those who are encountering this problem, it may be caused by an unnecessary “” character
in your Groovy Expression.

Nigel Thomas
on 19. September 2013 at 14:14 said:

Hi Timo,

I am trying to give the groovy expression into the default Value of the EO by selecting Expression Radio
button. But it is not accepting the value. Like , even after entering the value and pressing OK, the field
remains empty. Somehow after adding the expressionand tabbing out the field contains the expression.
But I cannot set the refresh Expression Value to true because it is disabled.
I am using Jdeveloper 12c.

Timo Hahn
on 20. September 2013 at 00:00 said:

There is a bug logged for this in jdev 12c at https://java.net/jira/browse/ADFEMG-163 which


describes a workaround too.

Aarav
on 11. April 2017 at 10:10 said:

how can i do the same to use the sequence in SQL Server.?


Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

This site uses Akismet to reduce spam. Learn how your comment data is processed.
Close and accept

https://tompeez.wordpress.com/2011/09/02/using-groovy-expression-to-set-a-primary-key-with-a-sequence-number/ 6/6

You might also like