留言板

ServiceBuilder + CounterLocalService

thumbnail
Hesam Pourjam,修改在13 年前。

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
Mika Koivisto,修改在13 年前。

RE: ServiceBuilder + CounterLocalService

Liferay Legend 帖子: 1519 加入日期: 06-8-7 最近的帖子
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.
Jeffrey Cox,修改在13 年前。

RE: ServiceBuilder + CounterLocalService

New Member 帖子: 11 加入日期: 11-2-9 最近的帖子
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
Mika Koivisto,修改在13 年前。

RE: ServiceBuilder + CounterLocalService

Liferay Legend 帖子: 1519 加入日期: 06-8-7 最近的帖子
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.
Elavarasu Pandiyan,修改在8 年前。

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
David H Nebinger,修改在8 年前。

RE: ServiceBuilder + CounterLocalService

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
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.