フォーラム

ホーム » Liferay Portal » English » 2. Using Liferay » General

構造的に表示 平面上に表示 ツリー上に表示
スレッド [ 前へ | 次へ ]
toggle
Danny Stevens
primary key clash with records inserted to DB outside of service layer?
2012/08/28 21:07
答え

Danny Stevens

ランク: New Member

投稿: 20

参加年月日: 2011/09/14

最近の投稿

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?
2012/08/29 7:43
答え

David H Nebinger

Community Moderator

ランク: Liferay Legend

投稿: 13099

参加年月日: 2006/09/01

最近の投稿

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?
2012/08/29 19:02
答え

Danny Stevens

ランク: New Member

投稿: 20

参加年月日: 2011/09/14

最近の投稿

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?
2012/08/30 19:25
答え

David H Nebinger

Community Moderator

ランク: Liferay Legend

投稿: 13099

参加年月日: 2006/09/01

最近の投稿

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?
2012/08/30 20:13
答え

Danny Stevens

ランク: New Member

投稿: 20

参加年月日: 2011/09/14

最近の投稿

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