Kombinierte Ansicht Flache Ansicht Baumansicht
Threads [ Zurück | Nächste ]
toggle
Danny Stevens
primary key clash with records inserted to DB outside of service layer?
28. August 2012 21:07
Antwort

Danny Stevens

Rang: New Member

Nachrichten: 20

Eintrittsdatum: 14. September 2011

Neue 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) ?
David H Nebinger
RE: primary key clash with records inserted to DB outside of service layer?
29. August 2012 07:43
Antwort

David H Nebinger

Rang: Liferay Legend

Nachrichten: 6533

Eintrittsdatum: 1. September 2006

Neue 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)...
Danny Stevens
RE: primary key clash with records inserted to DB outside of service layer?
29. August 2012 19:02
Antwort

Danny Stevens

Rang: New Member

Nachrichten: 20

Eintrittsdatum: 14. September 2011

Neue 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?
David H Nebinger
RE: primary key clash with records inserted to DB outside of service layer?
30. August 2012 19:25
Antwort

David H Nebinger

Rang: Liferay Legend

Nachrichten: 6533

Eintrittsdatum: 1. September 2006

Neue 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.
Danny Stevens
RE: primary key clash with records inserted to DB outside of service layer?
30. August 2012 20:13
Antwort

Danny Stevens

Rang: New Member

Nachrichten: 20

Eintrittsdatum: 14. September 2011

Neue Beiträge

Thanks David. I have asked the next obvious question in this thread.