Using JPA in SAP Java development

Note: first published on SCN on 29.2.2012

Java offers many technologies and mechanisms that help developers realize their ideas. One is to save data in a database without dealing with object relative mapping. From the several technologies available to save a Java object into a DB NetWeaver AS Java comes with SAP’s implementation of JPA. For the following blog I’m only looking into NetWeaver >= 7.1 as the code will be using annotations, available since Java 5.

The first step to save a Java object is to create the database table. The data stored will be the name and price of a product, as well as a unique id. For this, the Data Dictionary perspective in NWDS is used.

A table should have a primary key value and the columns defining the values that can be stored inside the Java object (this can also be done in the reverse way: create first the Java object and then the DB based on the object).

Second step is to create the Java object. The values that are going to be stored are mapped to the DB columns. This can be done automatically or by using the @Column annotation. The primary key column is special as the value of this column is normally determined by the application or database automatically without any interference of the user. For this the annotation @ID is used. Here the additional constraint of a table generated Id is used.




@NamedQuery(name=“findTest”, query=“SELECT p FROM Test p”)

class TestJPA implements Serializable {


    @TableGenerator(name=“TABLE_GEN_TEST”, table=“TMP_TEST_SEQ”, pkColumnName=“GEN_SEQ”,

     valueColumnName=“GEN_COUNT”, pkColumnValue = “TEST”)

    @GeneratedValue(strategy=TABLE, generator=“TABLE_GEN_TEST”)


    private String name;



  • @Table defines the table where the object values will be stored.
  • @NamedQuery is a select string to get all the object stored in the DB
  • The @ before the variable id define id as the Id (@Id) for JPA, how to store the id value (@GeneratedValue) and the actual value that will be used (@TableGenerator).

To automatically create the PK value for the ID column, SAP offers 3 alternatives:

  • AUTO

For table Id generation SAP Help states: “The TABLE ID generation strategy relies on the existence of a database table that manages ID values

  • The database table needs to exist!

In case the table isn’t specified, the default table TMP_SEQUENCE will be used. You have to ensure that either the default table or your custom table exists in the DB. Besides that, the table needs to contain specific elements (see the SAP Help link for more details).

  • AUTO defaults to TABLE sequence. The other 2 alternatives (IDENTITY and SEQUENCE) come with certain restrictions:
  • SEQUENCE needs a database sequence objects that has to be created manually at the DB
  • IDENTITY won’t work with OpenSQL and MS SQL server and IBM DB2.

TABLE sequence is the only way to create the ID by using the data dictionary and for every database. The table generator option for the ID value of your JPA tables requires a specific format for the sequence table:

  • primary key column needs to be of type varchar (Java: String)
  • the value column needs to be a number value

Creating a sequence table in NWDS for CE 7.1 in such a way is possible:

The table generator definition in the Java class works like this:

  • Name: defines the name of this table generator. Used by the @GeneratedValue part.
  • Table: defines the table where the sequence values are stored
  • valueColumnName: the name of the column where to look up the value. When the value found there is 56721 the value will be incremented by 1, thus the id will be 56722.
  • pkColumnName: name of the primary column of the table defined by table (TMP_TEST_SEQ). That value can be the name of the Java class or something else.

The JPA Details view shows all the information needed:

The object can be used as any other Java object. To store the data into the DB an entity manager is used. In the SAP scenario, this means that a data source and alias as well as a persistence unit need to be defined. The data source alias gets defined in the EAR project:

And in the EJB project:

The entity manager is using the persistence unit name to find out the data source. The data source is defined in the NWA of the AS Java and contains the information how to connect to the DB. The persistence unit gets defined in the EJB project. Java objects that are persisted normally are beans, so it makes sense to wrap the class in an EJB.

TestBean is the Bean, while TestLocal is the local interface for lookups. The actual business logic has to be implemented in the Bean, while adding the method to the local interface will make it public.

Hint: Using beans allows you to expose your business logic as a JSON object (with Apache Jersey). As beans can also be exposed remotely the Jersey server can run on another server.



class TestBean implements TestLocal {

@PersistenceContext(unitName = “PERM_UNIT”, type = PersistenceContextType.TRANSACTION)

    private EntityManager em;

void createTest(TestJPA test) {



    public List<TestJPA> getAllTest() {        

        List<TestJPA> test = em.createNamedQuery(“findAllTest”).getResultList();

        return test;



Local Interface:


interface TestLocal {

int createTest (TestJPA test);

    public List<TestJPA> getAllTest ();


Now the bean can be used in your Java code. In your SAP Portal application (PAR/WAR, of course, in WDJ too). Use the context and JNDI lookup to find the bean and start using it:

Context ctx = new InitialContext();

TestLocal testLocal = (TestLocal) ctx.lookup(“patlafldj TestLocal”);

Test test = testLocal.sldfjlajfdlskfj();

Let the world know

Making NWDS portable

Note: 1st published at SCN on 2.2.2012

Having to deal with several NetWeaver servers and NWDS installations? Want to make NWDS CE 7.1 portable to be more flexible? Read on …

Developing for NetWeaver AS Java means that you have to use the NetWeaver Developer Studio. The recommendation is to have for every NW release the corresponding NWDS version installed on your computer. When you have to take care of developments running on 7.0, 7.1 and 7.2 you end up having 3 NWDS versions installed.

NWDS for NetWeaver 7.0 can be downloaded at SCN. Until recently this was also true for CE. Hope that the CE 7.2 download comes back as it included a NWDS for CE 7.2.

For installing NWDS for CE 7.1 you have to download the installer from SAP. (S-User required)

That is not really complicated, but when you are switching workstations, travel or want to share your NWDS installation with a co-worker things get complicated. You have to download NWDS and install it. There are several pitfalls associated with this:

  • Time
  • administrator rights and
  • bandwidth.

Even when you have the time and bandwidth, you still need to have administrator privileges to install NWDS:

The NWDS for CE download link includes a step by step manual for installing NWDS CE. Step 3 is important: “The installer starts SAP NetWeaver Developer Studio automatically and invokes the Eclipse Update Manager where you can select the available NWDS features that you want to add to your installation.

This sentence means that you effectively install a basic Eclipse that needs to download and install all the features that transform Eclipse to NWDS. In my case, the basic NWDS CE 7.1 was roughly about 71 MB in size:

The preferences show that nothing SAP related is available.

No Java, Portal, Web Dynpro Java, DTR or any other feature and perspective related to SAP development available. Without these features you cannot develop for CE. That’s where step 3 comes in. To the 3 above mentioned possible pitfalls you have to add:

  • proxy configuration

If you corporate proxy isn’t permitting Eclipse to download the features you have a useless basic Eclipse installation. NetWeaver AS Java CE comes with an update site feature (http://server:port/updatesite) [1] that has to be configured by the administrator or by using another connection (home, hotel, other computer). After you managed to install NWDS CE 7.1, how do you get that working version portable? After downloading the features you may be tempted to zip the folder and to copy it to an external drive to make it available as a portable NWDS (it is Eclipse and Java after all). If you do so you will notice that the installation folder is still only 71 MB. So: where are the downloaded files? NWDS CE 7.1 won’t copy them to the eclipse/plugins|features folder. They are downloaded to [username]/.eclipse/ configuration/ eclipse

That’s where the downloaded SAP features are. This installation path makes it complicated to use NWDS when the administrator of your company installed the features: your user does not have access to the folder. Copying them from there to the eclipse directory to make Eclipse aware of them won’t work. Inside the above directory is a file called .eclipseextension. That’s the only hint you get from SAP, but it’s enough to identify it as an extension directory. Copy that directory to another location (e.g.: E:\CE). From there the SAP features can be installed.

The manage configuration dialog (Help -> Software Updates lists the features available.

To add the extension directory: open the context menu -> add -> extension location

Select the eclipse folder.

This will install the features. After a restart of NWDS all SAP features are available:

Done? Depends … if you are on Windows 7, of course not. A library will fail to load, so you’ll have to apply the solution provided by Dzintars Vilnis in the SCN forum:

Edit the configuration file SapNetweaverDeveloperStudio.ini.



<path to Java 1.5>\bin\javaw.exe










-DJDK.1.5.0_HOME=<path to Java 1.5> XP


What do you get after all that work? A NWDS for CE 7.1 that can be copied between computers.

Conclusion: that’s a little bit complicated considering the NWDS is Eclipse with features installed from SAP.

[1] Create Your Own Update Site Mirror for the NW Developer Studio

(That one is for 7.3, but the update site concept was already available for 7.1)

Let the world know