Forums de discussion

How to create composite primary key in Liferay6.1 using service builder

Raghu k, modifié il y a 11 années.

How to create composite primary key in Liferay6.1 using service builder

Junior Member Publications: 58 Date d'inscription: 10/08/12 Publications récentes
Hi,
I want to create a table whose primary key is a combination of below fields.
GROUPID, COMPANYID, USERID, FILEENTRYID
How can i create or define a composite primary key using service builder? When i checked in Liferay forum for this issue, i dint find any replies for it. Instead this bug is closed due to inactivity. Below is the link i checked.
http://issues.liferay.com/browse/LPS-4383
Is there any alternatives to do CRUD operations apart from service builder?
Can anyone help me here please.
thumbnail
David H Nebinger, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Liferay Legend Publications: 14914 Date d'inscription: 02/09/06 Publications récentes
Just add the columns to the SB entity and decorate them all with "primary='true'". The composite key will be created and can be used by your code.
Madasamy P, modifié il y a 7 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Junior Member Publications: 90 Date d'inscription: 27/07/16 Publications récentes
David H Nebinger:
Just add the columns to the SB entity and decorate them all with "primary='true'". The composite key will be created and can be used by your code.



I have two fields as,

<entity name="History" local-service="true" remote-service="true" uuid="true">
<!-- PK fields -->
<column name="historyId" type="long" primary="true"></column>
<column name="bookId" type="long" primary="true"></column>
<!-- UI fields -->
<column name="bookTitle" type="String"></column>
<column name="formName" type="String"></column>
<column name="fieldName" type="String"></column>
<column name="fieldValue" type="String"></column>
</entity>

After building the service, I want to find an entry by using historyId and bookId (composite key) as programmatically
How could to solve this
thumbnail
David H Nebinger, modifié il y a 7 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Liferay Legend Publications: 14914 Date d'inscription: 02/09/06 Publications récentes
Did you check the generated code? You should see getHistory() and fetchHistory() methods which take a composite key object as the argument.
Madasamy P, modifié il y a 7 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Junior Member Publications: 90 Date d'inscription: 27/07/16 Publications récentes
Thank you David
I checked and found the solution
thumbnail
Hitoshi Ozawa, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Liferay Legend Publications: 7942 Date d'inscription: 24/03/10 Publications récentes
http://issues.liferay.com/browse/LPS-4383


That's an old thread and as the comment in the issues states, it was probably just a request for assistance instead of a bug report.
Raghu k, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Junior Member Publications: 58 Date d'inscription: 10/08/12 Publications récentes
David and Hitoshi,
Thanks for your reply.
I have one more doubt.
Can we have 2 packages in a single service.xml? So that I can move classes generated by Service builder into respective package.
MICHAIL MOUDATSOS, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Regular Member Publications: 110 Date d'inscription: 04/10/11 Publications récentes
However this doesn't work:

	<entity name="FirstEntity" local-service="false" remote-service="false">
		<column name="firstEntityId" type="int" primary="true" />
		<!-- ... -->
	</entity>

	<entity name="SecondEntity" local-service="false" remote-service="false">
		<column name="secondEntityId" type="int" primary="true" />
		<!-- ...-->
		<column name="values" type="Collection" entity="ValueEntity" mapping-key="secondEntityId" />
	</entity>

	<entity name="ValueEntity" local-service="false" remote-service="false">
		<column name="firstEntityId" type="int" primary="true" />
		<column name="secondEntityId" type="int" primary="true" />
		<column name="value" type="String" />
	</entity>
Is it a bug (that I should - therefore - report) or is it supposed to behave this way?

EDIT: To avoid any confusion, it doesnt matter if we change firstEntityId to another name, in ValueEntity definition. It just happens to be my case, that's why I used FirstEntity's primary key name
thumbnail
David H Nebinger, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Liferay Legend Publications: 14914 Date d'inscription: 02/09/06 Publications récentes
MICHAIL MOUDATSOS:
However this doesn't work:

		<column name="values" type="Collection" entity="ValueEntity" mapping-key="secondEntityId" />



Of course it doesn't work. ValueEntity has a composite key, but you're trying to do a mapping using only one key from the primary key of ValueEntity.

Basically you're trying to manually implement a many to many relationship between the first entity and the second entity, which is just not going to work.

You should drop the collection from the second entity, add finders to the ValueEntity to find all records based on getting either the first key or the second key, and manually maintain the ValueEntity listings.
MICHAIL MOUDATSOS, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Regular Member Publications: 110 Date d'inscription: 04/10/11 Publications récentes
David H Nebinger:
Of course it doesn't work. ValueEntity has a composite key, but you're trying to do a mapping using only one key from the primary key of ValueEntity.
You 're so right! I almost forgot the most simple thing: I am doing an Entity Mapping! (as opposed to some db-wise parameterization) Thanks for the reply!
Raghu k, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Junior Member Publications: 58 Date d'inscription: 10/08/12 Publications récentes
I was able to build service.xml which had composite primaty without any issues. When did build, this generated required classes. But my service.xml also had finder methods.

When I checked my util class, i didn't find any method related to my finder methods. Am i missing anythong? Below is entity from my services.xml

<entity name="Favorites" local-service="true" remote-service="false" table="FAVORITE_DOCUMENTS">
<!-- PK fields -->
<column name="GROUPID" type="long" primary="true"></column>
<column name="COMPANYID" type="long" primary="true"></column>
<column name="USERID" type="long" primary="true"></column>
<column name="FILEENTRYID" type="long" primary="true"></column>
<!-- UI fields -->

<column name="CREATEDATE" type="Date"></column>

<finder name="GroupId" return-type="Collection">
<finder-column name="GROUPID" />
</finder>
<finder name="CompanyId" return-type="Collection">
<finder-column name="COMPANYID" />
</finder>
<finder name="UserId" return-type="Collection">
<finder-column name="USERID" />
</finder>
<finder name="singleFileOfUser" return-type="Collection">
<finder-column name="USERID" />
<finder-column name="GROUPID" />
<finder-column name="COMPANYID" />
<finder-column name="FILEENTRYID" />
</finder>
<finder name="allFilesForUGC" return-type="Collection">
<finder-column name="USERID" />
<finder-column name="GROUPID" />
<finder-column name="COMPANYID" />
</finder>

</entity>
Raghu k, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Junior Member Publications: 58 Date d'inscription: 10/08/12 Publications récentes
There are 2 util classes.
1.FavoritesUtil - which is in persistence package. This has my finder methods
2.FavoritesLocalServiceUtil - which is in service package. This has basic method not finder methods.

Which Util I should use in my class to perform required CRUD operation?
Raghu k, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Junior Member Publications: 58 Date d'inscription: 10/08/12 Publications récentes
I have taken below link as reference and did changes to FavoritesLocalServiceImpl by adding few methods which will intern call methods in FavoritesUtil. And then i build services.xml. But methods which i added in FavoritesLocalServiceImpl did not reflect in FavoritesLocalServiceUtil.
Can some one help me here please.
Link i referred:
http://www.liferay.com/community/forums/-/message_boards/message/4774829

Below is the code in FavoritesUtil
public static java.util.List<com.getransportation.cwc.webleads.model.Favorites> findByUserId(
long USERID) throws com.liferay.portal.kernel.exception.SystemException {
return getPersistence().findByUserId(USERID);
}

Below is the code from FavoritesLocalServiceImpl

public static List<Favorites> findByUserId(
long USERID)
throws SystemException {
return FavoritesUtil.findByUserId(USERID);
}
MICHAIL MOUDATSOS, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Regular Member Publications: 110 Date d'inscription: 04/10/11 Publications récentes
Raghu k:

Below is the code from FavoritesLocalServiceImpl

public static List<favorites> findByUserId(
			long USERID)
			throws SystemException {
			return FavoritesUtil.findByUserId(USERID);
}</favorites>
I think the error is that you define the method as static. To the best of my knowledge, you shouldn't do that. Remove the 'static' keyword, rebuild and check what happens
Raghu k, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Junior Member Publications: 58 Date d'inscription: 10/08/12 Publications récentes
MICHAIL MOUDATSOS:
I think the error is that you define the method as static. To the best of my knowledge, you shouldn't do that. Remove the 'static' keyword, rebuild and check what happens

I tried by removing static as well. Still no luck.
Raghu k, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Junior Member Publications: 58 Date d'inscription: 10/08/12 Publications récentes
David / Hitoshi / Michail,
Thanks for your reply. It is working fine now. Problem is with Eclipse. It is working after removing static.
thumbnail
David H Nebinger, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Liferay Legend Publications: 14914 Date d'inscription: 02/09/06 Publications récentes
Raghu k:
When I checked my util class, i didn't find any method related to my finder methods. Am i missing anythong?


Finders are only generated in the persistence layer. To expose them in the util class, you need to add them to the XxxLocalServiceImpl class and re-run service builder.

Kind of a pain, but I think the view is that the finder is to be used within the XxxLocalServiceImpl class and not normally exported. Think of your XxxLocalServiceImpl class as an implementation of business logic, and not just a facade over the persistence layer.

That's actually a good approach to take in most cases, because you don't want to have to replicate business logic in all of the portlets consuming the service.
Raghu k, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Junior Member Publications: 58 Date d'inscription: 10/08/12 Publications récentes
David H Nebinger:

Finders are only generated in the persistence layer. To expose them in the util class, you need to add them to the XxxLocalServiceImpl class and re-run service builder.
.

I added my method in my LocalServiceImpl class and did build as well. This build was successful. But my method in XxxLocalServiceImpl class was not available in my XxxLocalServiceUtil class.
My XxxLocalServiceImpl class with custom method is available in my earlier post.
thumbnail
David H Nebinger, modifié il y a 11 années.

RE: How to create composite primary key in Liferay6.1 using service builder

Liferay Legend Publications: 14914 Date d'inscription: 02/09/06 Publications récentes
Raghu k:
Can we have 2 packages in a single service.xml? So that I can move classes generated by Service builder into respective package.


No. Service builder uses the one package defined in service.xml for the base package of all classes. If you want a different package, you need a second plugin to host the second service.xml file.