Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Sergio Cabrera
One-to-one relationship in Service Builder
January 16, 2012 5:22 AM
Answer

Sergio Cabrera

Rank: Junior Member

Posts: 68

Join Date: December 19, 2011

Recent Posts

Hello, I'm trying to implement a one-to-one relationship in Service Builder. I've seen this issue but I have a doubt. There it says that in the example I could add a getProducto() method to FormatoImpl:

1public class FormatoImpl {
2   public Producto getProducto() {
3       if (idProducto == 0) {
4          return null;
5       }
6       return ProductoLocalServiceUtil.getProducto(idProducto);
7   }
8}


Where do I get the idProducto? What should I put in service.xml? Thank you
David Ilechukwu
RE: One-to-one relationship in Service Builder [URGENT]
January 19, 2013 10:16 AM
Answer

David Ilechukwu

Rank: Regular Member

Posts: 114

Join Date: June 7, 2010

Recent Posts

Hey guys, I'm having exactly the same issue over here (thought thsi may have been resolved in 6.1.1 - was kinda shocked to see it's still there).

This is what I've managed to do (not sure if it's the right way)
1
2    public Producto getProducto(Integer idProducto) {
3        if(idProducto== 0){
4            return null;
5        }
6        return ProductoLocalServiceUtil.getProducto(idProducto);
7    }


However the "return ProductoLocalServiceUtil.getProducto(idProducto);" line complains about unreported exceptions SystemException,PortalException.

Anybody know where exactly this was used in the Liferay Core?
Is there another method of defining One-to-One Mapping?
(I am using Liferay 6.1.1 (GA2)

Thanks for quick response, guys!
David H Nebinger
RE: One-to-one relationship in Service Builder [URGENT]
January 20, 2013 8:06 PM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7250

Join Date: September 1, 2006

Recent Posts

There is nothing wrong w/ service builder. The XxxLocalServiceUtil static methods may always throw those exceptions; if you're not going to throw them, you need to handle them.

As far as the method signature goes, you're both wrong.

The OP didn't include any key value at all; how do you select a single row from a table with no key? You don't. SB is no different here.

Peter's a little closer, using:

1 public Producto getProducto(Integer idProducto) {
2   if (idProducto == 0) ...


Line 2, however, is wrong. What happens if someone passes in null for the Integer? Since your'e using the object form, it could happen...
David Ilechukwu
RE: One-to-one relationship in Service Builder
January 23, 2013 12:17 AM
Answer

David Ilechukwu

Rank: Regular Member

Posts: 114

Join Date: June 7, 2010

Recent Posts

Hi Sergio,
I managed to sort out this problem. Please take the steps below to implement 1-to-1 mapping using ServiceBuilder:
(1.) First of all, add the productoId field as a foreign key field to your Formato entity SB definition like this:
 1
 2    <entity name="Formato" local-service="true" remote-service="false">
 3        <!-- PK fields -->
 4        <column name="formatoId" type="long" primary="true"></column>
 5        <!-- FK fields -->
 6        <column name="productoId" type="long"></column>
 7        <!-- Other fields -->
 8        <column name="companyId" type="long"></column>
 9        <column name="groupId" type="long"></column>
10    </entity>


(2) Then after running ServiceBuilder sucessfully - open the generated FormatoImpl class, and add the following:
1
2    public Producto getProducto() throws PortalException, SystemException{
3        return ProductoLocalServiceUtil.getProducto(getProductoId());
4    }


Works like a charm. I also found out that's the way 1-to-1 mappings are done in Liferay Core.
Please let me know if it works - or if you encounter any other problems.

I also apologize for delayed response to your post (more thana year now). Hopefully, things would improve in the future.

Regards
David Ilechukwu
RE: One-to-one relationship in Service Builder
January 28, 2013 2:31 AM
Answer

David Ilechukwu

Rank: Regular Member

Posts: 114

Join Date: June 7, 2010

Recent Posts

Hi Sergio -
Please let us know if above method resolved your problem?

Regards, David
Sergio Cabrera
RE: One-to-one relationship in Service Builder
May 23, 2013 9:04 AM
Answer

Sergio Cabrera

Rank: Junior Member

Posts: 68

Join Date: December 19, 2011

Recent Posts

Sorry, I've seen this answer now, but in fact my first question was in January but... in 2012!

Thanks anyway