Concept of ClassPK in liferay

concept-of-classPK-in-liferay


aimClassPK in Liferay can be confusing if its not understood properly. Along with classPK in liferay there is another term called classNameId which are commonly used. This article explains the nut and bolts of classPK in Liferay. We will also explain how to analyze Liferay Tables where classPKs are used as a column name. 


Fact 1: 

Every Entity generated by Liferay Service Builder Tool has its unique ID which is called classNameId. Consider that we have our custom portlet where we have used Service Builder Tool to generate code. So when the portlet is deployed for the first time the name of the Entity will be registered in a table and classNameId will be also generated. Below is the table (i.e., classname_) where all the registered Entity’s  are persisted. 

classname_

Some rows of classname_ table:

data_classname_

[focus]

The classNameId as well as value are unique in the classname_ table. Even combining classNameId and value column the record is always unique in classname_ table.

[/focus]

Fact 2: 

There are many tables in liferay default database lportal. We can see classPK as column name in some of those tables. So what should we interpret by looking classPK as column name?

In simple classPK represents primary keys of a records which are in different table. That means classPK just like a foreign key reference. 

Fact 3: 

We understood by Fact 2 mentioned in above that classPKs are just like a foreign key reference to some other table. So how would we know table name for which we have the classPK. In other words by classPK how would I find corresponding record in other table.

The answer is simple. The tables where classPK column is available, classNameId would be also available in the same table. By classNameId we can give a look up in classname_ table to find out corresponding entity name. If we know the entity name then we can easily find out table name associated with the entity name.   

Example:

Try assetentry table of liferay and follow the below strategy.  

lookup_classname_


Liferay Service for classname_ table:

Some code 

ClassName className = ClassNameLocalServiceUtil.getClassName("com.liferay.portlet.expando.model.ExpandoRow");
ClassName classNamee = ClassNameLocalServiceUtil.getClassName(10107);
long classNameId = ClassNameLocalServiceUtil.getClassNameId("com.liferay.portlet.expando.model.ExpandoRow");

About The Author

3 thoughts on “Concept of ClassPK in liferay”

  1. Hey,

    nice explanation. I nedded much time to understand this on my own. 🙂
    Maybe you also know a probability to add new className_ entries via a config file like portal-ext.properties or portlet.xml to add some portlet specific classNames?

    A programatical way is fine too but I think I can figure this out in my own.

    Regards,

    Tobias

Leave a Reply

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

Scroll to Top
%d