Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
mohammad azaruddin
Adding to common database table from two different method
March 19, 2013 10:29 PM
Answer

mohammad azaruddin

Rank: Expert

Posts: 443

Join Date: September 17, 2012

Recent Posts

Hi All
I'm adding data to common database table from two different method..AND depending upon my requirnment i use any one method at a time..and in both method i write code to create recID(unique).so my doubt is when we write code to create recId(uniue key) in both the method,whether it ll be able to create unique key....?

thanks
regards
azharemoticon
Samuel Kong
RE: Adding to common database table from two different method
March 19, 2013 11:23 PM
Answer

Samuel Kong

LIFERAY STAFF

Rank: Liferay Master

Posts: 959

Join Date: March 10, 2008

Recent Posts

You have a few options

1) Have your two methods call a common method.

2) If your database supports it, let the database generate the ID.

3) Use Liferay's CounterLocalServiceUtil to generate an ID.
Hitoshi Ozawa
RE: Adding to common database table from two different method
March 19, 2013 11:27 PM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7990

Join Date: March 23, 2010

Recent Posts

Just create two methods in Impl methods.

whether it ll be able to create unique key....?


That'll probably depend on how you're creating you key. Look at the online Liferay Development documents on Service Builder example.
mohammad azaruddin
RE: Adding to common database table from two different method
March 19, 2013 11:30 PM
Answer

mohammad azaruddin

Rank: Expert

Posts: 443

Join Date: September 17, 2012

Recent Posts

I dint get answer for my previous question.but solved it

Instead of setting recId manually i create recId using service.xml....
<column name="RecId" type="long" primary="true" id-type="identity" />


here is the doc
mohammad azaruddin
RE: Adding to common database table from two different method
March 19, 2013 11:33 PM
Answer

mohammad azaruddin

Rank: Expert

Posts: 443

Join Date: September 17, 2012

Recent Posts

Hi Samuel Kong
Now i'm using 2nd option and it worksemoticon
thank you
Regards
azhar
Hitoshi Ozawa
RE: Adding to common database table from two different method
March 19, 2013 11:33 PM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7990

Join Date: March 23, 2010

Recent Posts

Few cautions on Samuel's suggestion.

1) Have your two methods call a common method.


This may not work on clustered environment.

2) If your database supports it, let the database generate the ID.


This will slow the system down on a heavily used system.

3) Use Liferay's CounterLocalServiceUtil to generate an ID.


Probably the best option unless you mind it becoming liferay dependent.
mohammad azaruddin
RE: Adding to common database table from two different method
March 19, 2013 11:43 PM
Answer

mohammad azaruddin

Rank: Expert

Posts: 443

Join Date: September 17, 2012

Recent Posts

Hi
Hitoshi Ozawa

I was creating pKey using this method
long recordid = 1L;
recordid = CounterLocalServiceUtil.increment(this.getClass()
.getName());


my doubt was if i use above code in two different method to generete pKey but adding to a single table(not at same time), whether it ll be able to create unique key.
and now i use 2nd option as mentioned by LR staff

Thanks