掲示板

ServiceBuilder + CounterLocalService

thumbnail
12年前 に Hesam Pourjam によって更新されました。

ServiceBuilder + CounterLocalService

New Member 投稿: 6 参加年月日: 06/12/24 最新の投稿
Hey Guys,

I am working on building portlets using ServiceBuilder + Vaadin Framewrok.

i used plugin-sdk to build portlet and models/services through service builder mechanism
everythings worked fine but when i tried to add a record by MyEntityLocalServiceUtil i got an excpetion about Duplicate Entry , i found out that the AddMyEntity method doesnt generate an ID so i looked at native Liferay classes and saw that they are using counterLocalService.incremet() method to generate long IDs, i tried that but i got an NULL counterLocalService in my MyEntityLocalServiceBaseImpl class i tried to add counter-spring.xml to the META-INF folder and add it to service.properties but it is still null and i dont know what to do, i have got really confused.

how should i use that ? why hibernate id generators dont work for this purpose ?

Thanks in advance ...
thumbnail
12年前 に Mika Koivisto によって更新されました。

RE: ServiceBuilder + CounterLocalService

Liferay Legend 投稿: 1519 参加年月日: 06/08/07 最新の投稿
Service Builder automatically generates reference to CounterLocalService to XXXLocalServiceBaseImpl. In your impl class you can generate a pk like this:
long pk = counterLocalService.increment();


We don't rely on hibernate id generator because our solution works more consistently across different databases.
12年前 に Jeffrey Cox によって更新されました。

RE: ServiceBuilder + CounterLocalService

New Member 投稿: 11 参加年月日: 11/02/09 最新の投稿
What if I have table for which I want to externally load data. For example I have some data that I would be migrating from an older system, but need to maintain the ID's such that they continue to correspond with the appropriate records from other tables. Is it possible to set where the the auto increment starts? or how does the increment work?


Thanks in advance
thumbnail
12年前 に Mika Koivisto によって更新されました。

RE: ServiceBuilder + CounterLocalService

Liferay Legend 投稿: 1519 参加年月日: 06/08/07 最新の投稿
You can create a separate counter for each entity also. Just pass in the class name to increment. Like:
long pk = counterLocalService.increment(MyEntity.class.getName());


You should NEVER write to the tables directly. All writes should go through the service layer.
8年前 に Elavarasu Pandiyan によって更新されました。

RE: ServiceBuilder + CounterLocalService

New Member 投稿: 8 参加年月日: 15/10/14 最新の投稿
I am facing the same issue as Jeffrey stated.
I have migrated a database, created entities for the existing tables.
On inserting data as above, the id generated starts from 1 rather than from the existing latest id in the table.

Though, temporarily I resolved the issue by updating the currentId value for the entity in the Counter table.

Could you please help me here.
thumbnail
8年前 に David H Nebinger によって更新されました。

RE: ServiceBuilder + CounterLocalService

Liferay Legend 投稿: 14914 参加年月日: 06/09/02 最新の投稿
Um, if you're starting w/ a new database then the key should start with 1. If you've migrated an existing database then the keys set in the existing database would be some number higher than 1.

I'm wondering if perhaps you didn't try some sort of "partial" migration. That could result in the keys being 1 (because Liferay thinks it's a new database) but you needing a larger value. In this case your only option is to initialize the counter table to the value you want to start at.

Note that if you now have two separate Liferay instances pointing at some shared table area, each instance would be managing it's own keys separately, so ultimately you're going to have a mess of key conflicts all the time.