Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Carl O Ellam-Speed
Service Builder transaction processing - returning my object
July 4, 2013 8:05 AM
Answer

Carl O Ellam-Speed

Rank: New Member

Posts: 22

Join Date: December 23, 2009

Recent Posts

Hi,

I really need help, but don't quite know where to start. Therefore, I'll try to be as clear as I can:

I've managed to create four tables in MySQL and create a set of Utlity classes with Service Builder for them.

I can insert, update and delete using their individual utility methods. All well and good!

Now, I want to ensure that transactional state is maintained when inserting details into the tables, i.e. I have a CLUB, CONTACT_DETAILS and ADDRESS tables. I need inserts to occur to all three when I need to.

I've created an additional Entity, ClubTX, for which I've proven that I can create an InsertClub method that can insert details to the three tables.

However, in the ClubTxLocalServiceImpl method, I want a fetchClub method, that goes to each of the three tables and gets the details back. Data is returned to each model, of Club, ContactDetails and Address. However, I want to return all three to the calling portlet.

If I try to create my own helper class, called FullClubRecord, that I then populate in my fetchClub method, all is well. However, I need to rebuild services because the method footprint (i.e. return has changed).

When it builds, it hits compilation issues. My helper class can't be found. I've put it in a new package within the same project. The ClubTxLocalServiceImpl can see the class, but the other generated classes fail to and fail with a cannot be resolved to type.

Am I trying to do something that isn't possible?

Any recommendations would be appreciated.

kind regards

speedy

P.S. I can see that if I return an object[] I can put the three objects back this way, but it's not ideal. The service builder is then fine.
David H Nebinger
RE: Service Builder transaction processing - returning my object
July 5, 2013 9:35 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11115

Join Date: September 1, 2006

Recent Posts

SB code will only allow you to return entities, primitives (and their object counterparts), and collections. You cannot define your own class to return in SB code.

You can, however, define a 'fake' entity which contains the three actual entities you want to return. The fake entity would satisfy SB code (since it's an entity).

You can see how to define a fake entity here and here.
Carl O Ellam-Speed
RE: Service Builder transaction processing - returning my object
July 10, 2013 4:35 AM
Answer

Carl O Ellam-Speed

Rank: New Member

Posts: 22

Join Date: December 23, 2009

Recent Posts

Thanks David,

I resorted to passing and returning HashMap objects of my records, which works very well for my purposes. It's not ideal, but it works. Thanks for the heads up on the fake entities, I will look into that.
David H Nebinger
RE: Service Builder transaction processing - returning my object
July 10, 2013 4:40 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11115

Join Date: September 1, 2006

Recent Posts

Hashmap may pose issues for separate service consumers...

When using the SB code from a different WAR, you're crossing the web application boundary the server puts up. The SB code handles serializing/deserializing across that boundary for types it knows about, but can result in failure for types it does not (I believe hashmap is one of those).
Milen Dyankov
RE: Service Builder transaction processing - returning my object
July 19, 2013 7:09 AM
Answer

Milen Dyankov

LIFERAY STAFF

Rank: Regular Member

Posts: 237

Join Date: October 30, 2012

Recent Posts

I'm not quite sure what you are asking for, but if your only concern are transactions then you get those OOTB with ServiceBuilder.
Each method in LocalServiceImpl is executed in transaction and if inside you call a method in [AnotherEntityName]LocalServiceImpl it will join the transaction.

For example assuming you have ClubLocalServiceImpl, ClubAddressLocalServiceImpl and ClubContactLocalServiceImpl you can add a saveMyClub method in ClubLocalServiceImpl like this:

 1pubic void saveMyClub (.....) {
 2   ...
 3   clubPersistence.save(...)
 4   ...
 5   // calling service layer to apply custom logic
 6   clubAdressLocalServiceImpl.save(...) 
 7   ...
 8   // calling persistence layer directly
 9   clubContactPersistence.save(...)
10}


Run service builder again and when you call ClubLocalServiceUtil.saveMyClub(...) all three tables will be updated in the same transaction.