Foros de discusión

CounterLocalServiceUtil.increment();

thumbnail
Jennis Vaishnav, modificado hace 7 años.

CounterLocalServiceUtil.increment();

Junior Member Mensajes: 59 Fecha de incorporación: 1/02/17 Mensajes recientes
I have connected Mysql database in a liferay portlet, i have configured service.xml accordingly, the problem i'm facing is
long id=CounterLocalServiceUtil.increment();
is giving me different id's but when i look in database by select * from tableName; gives different ids not starting from 0.
Is there a way to set CounterLocalServiceUtil.increment() to 0?
thumbnail
Olaf Kock, modificado hace 7 años.

RE: CounterLocalServiceUtil.increment();

Liferay Legend Mensajes: 6403 Fecha de incorporación: 23/09/08 Mensajes recientes
Jennis Vaishnav:
Is there a way to set CounterLocalServiceUtil.increment() to 0?


No. It's designed to give you unique values. If you need control over those numbers, you'll have to roll your own - just don't use them as primary keys if you also plan to utilize Liferay's permission checker on your objects, because this would introduce duplicate keys.
thumbnail
Jennis Vaishnav, modificado hace 7 años.

RE: CounterLocalServiceUtil.increment();

Junior Member Mensajes: 59 Fecha de incorporación: 1/02/17 Mensajes recientes
Thanks for the reply Olaf, The generated ID from CounterLocalServiceUtil.increment() is not even in proper order(the gap between ID is irregular,though in ascending order), so if we try to perform read and update operation which are done using ID field only, than how It can be done for reading and update operations in database! I'm just afraid that it wont be throwing garbage values to the column ID. emoticon
-Thanks!
thumbnail
Danielle Ardon, modificado hace 7 años.

RE: CounterLocalServiceUtil.increment(); (Respuesta)

Junior Member Mensajes: 37 Fecha de incorporación: 6/06/16 Mensajes recientes
Hi Jennis,

I believe this is because CounterLocalServiceUtil.increment() uses a global counter (you can check this in the database table called counter).
This counter is used for a lot of different entities, so the gaps are caused by other entities being added to the database.

You could try the CounterLocalServiceUtil.increment(String name) method. For example, you could add the fully qualified class name of your entity. This should start with 0.
Hopefully this helps.

See also:
https://web.liferay.com/community/forums/-/message_boards/message/12554607

I do wonder what you mean by updating and reading based on id field only? And why won't that work if the id's are irregular?
thumbnail
Jennis Vaishnav, modificado hace 7 años.

RE: CounterLocalServiceUtil.increment();

Junior Member Mensajes: 59 Fecha de incorporación: 1/02/17 Mensajes recientes
Danielle Ardon:

I do wonder what you mean by updating and reading based on id field only? And why won't that work if the id's are irregular?

Hi Danielle ,
The reason behind why it wont work is,Suppose i'll update a record by id by selecting it from a drop down menu which would be full of irregularities, just to get ease of access I'm thinking to get id in a proper way! There's no major reason behind it!
Thank you for the answer it worked!
thumbnail
Olaf Kock, modificado hace 7 años.

RE: CounterLocalServiceUtil.increment();

Liferay Legend Mensajes: 6403 Fecha de incorporación: 23/09/08 Mensajes recientes
Jennis Vaishnav:
...which would be full of irregularities, just to get ease of access I'm thinking to get id in a proper way!...


Note that the definition of "id" is: You never get the same value twice. Period. - no other assumption can be made. Not even order can be assumed (though "Counter" implies order) Definitely no "+1 stepping" or "start at 0" is part of any assumption made by this concept.

If you want any of this: Roll your own.