Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
Getting primary key from result of XXXXLocalServiceUtil.addXXX(..)
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??
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);
Powered by Liferay™