Message Boards

Getting primary key from result of XXXXLocalServiceUtil.addXXX(..)

Kevin Neibarger, modified 2 Years ago.

Getting primary key from result of XXXXLocalServiceUtil.addXXX(..)

Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts

So, I'm having issues with the "add" methods of servicebuilder methods. I have a database table that holds an alert record, and it's primary key "alert_id" is auto incremented, so everytime you insert a record it inserts with the next incremented value. When I build an Object to add to the Entity/Table

Alerts a = AlertsLocalServiceUtil.createAlerts(0);

And then I do the "add" method with the returned value

Alerts added = AlertsLocalServiceUtil.addAlerts(a);

When I print out added.getAlertId() it's always 0. It should be the next incremented value. I need it to add a record to another table.

My only solution is to query the current table for the last inserted record and get the alertId. That's killing performance as you can imagine.. 

Why doesn't added.getAlertId() return the alertId inserted into the DB Table?? 

thumbnail
Russell Bohl, modified 2 Years ago.

RE: Getting primary key from result of XXXXLocalServiceUtil.addXXX(..)

Expert Posts: 291 Join Date: 2/13/13 Recent Posts

I'm not entirely certain what you're running into, but the pattern you're using doesn't look like the one we use in Liferay.

There are useful services that are injected for you to use in your service impl.

To generate and increment the ID use long alertId = counterLocalService.increment();

To create the entity, use alertsPersistence.create(alertId);