Skip to main content
  1. blog/

Understanding the PK in SAP Hybris Commerce

·3 mins

Overview #

A source of confusion among developers new to Hybris is the concept of the PK. The PK is important to understanding how the Hybris persistence layer works.

In this article, I’ll discuss how a Hybris PK differs from a standard database primary key and how it is used in the Hybris platform.


Traditional PK vs. Hybris PK #

In a relational database, a primary key is a column that uniquely identifies each row in the table. These are typically auto-incrementing fields starting with 1.

In contrast, a Hybris primary key (PK) is a 13-digit number that is unique across the entire database. The number has data encoded inside it. This is part of the proprietary ORM solution.

Primary Key and type code #

The typecode is an internal ID for the Hybris type. It can’t be changed once the type was created because the primary keys of the items contain the typecode.

The typecode ranges 0-10099, 13200-13299, 24400-24599 and 32700-32799 are reserved for internal Hybris’ use and must not be used to avoid conflicts with new Hybris out-of-the-box extensions

Every item has the primary key (PK), creation time, modified time, itemtype (read-only) attributes which are mapped to columns in the table and managed by Hybris.

There is no way to generate valid PKs outside the Hybris platform and the PK of an item of one database could be invalid in other database. Hybris has internal counters for PKs which aren’t accessible to the developers. If we need to import data into another system, we must use the Impex so Hybris can generate the PK during the import.


Getting the PK using Java #

Every model class contains a method called getPk() inherited from the AbstractItemModel class which we can use to retrieve the PK:

public PK getPk() { 
  return this.getItemModelContext().getPK();
}

Notice that the return value is not numeric but a PK object. The PK class has methods like getLong(), getLongAsString(), and so on. Here is the JavaDoc.


Can a Hybris PK be generated outside of Hybris? #

No. It is not possible to generate a PK outside of Hybris. The PK of an item in one database could be invalid in another database. Hybris has internal counters for PKs which aren’t accessible to developers.

If we need to import data into another system, we need to use Impex so Hybris can generate the PK during the import.


Determining the Item Type using a PK #

It’s easy to find out what item type a PK belongs to.

HAC #

In Hybris Administration Console (HAC), go to Platform > PK Analyzer:

HAC PK Analyzer

HMC #

In HMC, we can modify the URL to open the PK details page directly:

https://<server>/hmc/hybris?open=<PK>