Foren

primary key clash with records inserted to DB outside of service layer?

thumbnail
Danny Stevens, geändert vor 11 Jahren.

primary key clash with records inserted to DB outside of service layer?

New Member Beiträge: 20 Beitrittsdatum: 15.09.11 Neueste Beiträge
Lets take the use case of moving a company record from one liferay database to a completely different portal's database. There is a good chance the company primary key will clash. I can easily write code to give the incomming record a new primary key and sort out foreign key references later. However, how can I do this so that liferay wont try to re-use that key when creating a future company (portal instance) ?
thumbnail
David H Nebinger, geändert vor 11 Jahren.

RE: primary key clash with records inserted to DB outside of service layer?

Liferay Legend Beiträge: 14914 Beitrittsdatum: 02.09.06 Neueste Beiträge
You cannot.

Liferay leverages the CounterLocalServiceUtil to generate primary keys. You'd have to update the counter local value on the source side, but even then there would still be potential conflicts (source side is very active so it's up to counter value 150, target site is not as active so it is only at counter value 27)...
thumbnail
Danny Stevens, geändert vor 11 Jahren.

RE: primary key clash with records inserted to DB outside of service layer?

New Member Beiträge: 20 Beitrittsdatum: 15.09.11 Neueste Beiträge
Hi David,

thanks heaps for that response. I think I now know how to go forward but I need to know more about how the CounterLocalService is properly used.

Here is some pseudo code for what I'm planning:

1 read in a record from my export file for some table x which has a surrogate key y
2 ask the CounterLocalService for an appropriate new key z
3 create a mapping from the old key y to new key z for adjusting foreign key references later
4 write the record with the new key z and all foreign keys to other records adjusted to whatever their new key mappings are

To be able to do step 2 I need to know the rules for using CounterLocalServiceUtil, which has no javadoc or signiature contracts. Where could I go for this information?
thumbnail
David H Nebinger, geändert vor 11 Jahren.

RE: primary key clash with records inserted to DB outside of service layer? (Antwort)

Liferay Legend Beiträge: 14914 Beitrittsdatum: 02.09.06 Neueste Beiträge
Good question.

Liferay uses it for (I believe) all inserts of records. Many of their inserts use the generic counter, but some (i.e. layout related) use a specific counter.

If it's your own counter you'd be using, you would call CounterLocalServiceUtil.increment(MyClass.class.getName()) to return the next counter value. You can also use the CounterLocalServiceUtil methods to access other counters, and the standard is to use the class name for the counter name.
thumbnail
Danny Stevens, geändert vor 11 Jahren.

RE: primary key clash with records inserted to DB outside of service layer?

New Member Beiträge: 20 Beitrittsdatum: 15.09.11 Neueste Beiträge
Thanks David. I have asked the next obvious question in this thread.