Tribune

Home » Liferay Portal » English » 2. Using Liferay » General

Vista Combinata Vista Piatta Vista ad Albero
Discussioni [ Precedente | Successivo ]
toggle
Danny Stevens
primary key clash with records inserted to DB outside of service layer?
28 agosto 2012 21.07
Risposta

Danny Stevens

Punteggio: New Member

Messaggi: 20

Data di Iscrizione: 14 settembre 2011

Messaggi recenti

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 agosto 2012 7.43
Risposta

David H Nebinger

Punteggio: Liferay Legend

Messaggi: 7078

Data di Iscrizione: 1 settembre 2006

Messaggi recenti

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 agosto 2012 19.02
Risposta

Danny Stevens

Punteggio: New Member

Messaggi: 20

Data di Iscrizione: 14 settembre 2011

Messaggi recenti

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 agosto 2012 19.25
Risposta

David H Nebinger

Punteggio: Liferay Legend

Messaggi: 7078

Data di Iscrizione: 1 settembre 2006

Messaggi recenti

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 agosto 2012 20.13
Risposta

Danny Stevens

Punteggio: New Member

Messaggi: 20

Data di Iscrizione: 14 settembre 2011

Messaggi recenti

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