Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Hangover Liferay
Liferay service.xml foreign key many-to-many
November 19, 2012 12:53 PM
Answer

Hangover Liferay

Rank: New Member

Posts: 17

Join Date: May 18, 2010

Recent Posts

Hello Liferay experts, Please help me to create a service.xml in Liferay for the following scenario

I have 2 tables

1) SystemUsed
systemUsedId long name varchar

2) User_SystemUsed
userId long --> mapped to userId of User_ table
systemUsedId long --> mapped to systemUsedId of SystemUsed table.

Thanks
Micheal
Vipin Bardia
RE: Liferay service.xml foreign key many-to-many
November 19, 2012 1:12 PM
Answer

Vipin Bardia

Rank: Regular Member

Posts: 109

Join Date: February 27, 2011

Recent Posts

You can define one to many in service.xml like below example , but i am unaware of many to many relationship with service.xml in liferay.
It's probably a bug, but if you got any answer please update.

<service-builder package-path="com.service">
<author>vipin.bardia</author>
<namespace>lsm</namespace>

<entity name="SystemUsed " local-service="true" remote-service="false">
<!-- PK fields -->
<column name="systemUsedId" type="long" primary="true" />
<column name="name" type="String" />
<column name="UserList" type="Collection" entity="SystemUsed" mapping-key="systemUsedId" />
</entity>

<!-- One to Many Mapping -->
<entity name="User_SystemUsed" local-service="true" remote-service="false">
<!-- PK fields -->
<column name="userId" type="long" primary="true" />

<!-- Ref Key for one to many mapping -->
<column name="systemUsedId" type="long" />
</entity>
</service-builder>

Write down below method in SystemUsedLocalServiceImpl -

public List<SystemUsed> getByUserId(long userId){
SystemUsedUtil.getSystemUsed(userId);
}


Regards,
Vipin Bardia
Juhi Kumari
RE: Liferay service.xml foreign key many-to-many
November 19, 2012 9:48 PM
Answer

Juhi Kumari

Rank: Expert

Posts: 347

Join Date: December 12, 2011

Recent Posts

Hi,

You can define the same column name and their datatype as they defined in mapped table
and give the references of those tables.
<entity name="User_SystemUsed " local-service="true" remote-service="true">
<column name="userId" type="long" />
<column name="systemUsedId" type="long" />

<!-- References -->

<reference package-path="com.liferay.portal" entity="User" />
<reference package-path=".........." entity="SystemUsed" />
</entity>


Regards
Juhi
Hangover Liferay
RE: Liferay service.xml foreign key many-to-many
November 20, 2012 3:18 PM
Answer

Hangover Liferay

Rank: New Member

Posts: 17

Join Date: May 18, 2010

Recent Posts

Thanks for your response, Here is my Service.xm file. When I try to build the services I get an exception. See below for the log.

Thanks
-M

service.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.test.t.service">
<author>test</author>
<namespace>ABC</namespace>

<entity name="SystemUsed" local-service="true" remote-service="false">
<!-- PK fields -->
<column name="systemUsedId" type="long" primary="true" />
<!-- Other fields -->
<column name="name" type="String" />

<!-- Order -->
<order by="asc">
<order-column name="name" />
</order>
<!-- Finder methods -->
<finder name="name" return-type="Collection">
<finder-column name="name" />
</finder>
</entity>

<entity name="User_SystemUsed" local-service="true" remote-service="false">

<!-- Other fields -->
<column name="userId" type="long" primary="true" />
<column name="systemUsedId" type="long" primary="true" />

<!-- References -->

<reference package-path="com.liferay.portal" entity="User" />
<reference package-path="com.test.t.service" entity="SystemUsed" />

<!-- Order -->
<order by="asc">
<order-column name="systemUsedId" />
</order>
<!-- Finder methods -->
<finder name="userId" return-type="Collection">
<finder-column name="userId" />
</finder>
<finder name="systemUsedId" return-type="Collection">
<finder-column name="systemUsedId" />
</finder>

</entity>

</service-builder>
===================================================================

Exception

com.liferay.portal.kernel.xml.DocumentException: Error on line 48 of document : The content of element type "entity" must match "(column*,order?,finder*,reference*,tx-required*)". Nested exception: The content of element type "entity" must match "(column*,order?,finder*,reference*,tx-required*)".
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:399)
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:415)
at com.liferay.portal.kernel.xml.SAXReaderUtil.read(SAXReaderUtil.java:161)
at com.liferay.portal.tools.servicebuilder.ServiceBuilder.<init>(ServiceBuilder.java:561)
at com.liferay.portal.tools.servicebuilder.ServiceBuilder.main(ServiceBuilder.java:151)
Caused by: org.dom4j.DocumentException: Error on line 48 of document : The content of element type "entity" must match "(column*,order?,finder*,reference*,tx-required*)". Nested exception: The content of element type "entity" must match "(column*,order?,finder*,reference*,tx-required*)".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.dom4j.io.SAXReader.read(SAXReader.java:365)
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:396)
... 4 more

BUILD FAILED
Hangover Liferay
RE: Liferay service.xml foreign key many-to-many
November 20, 2012 3:19 PM
Answer

Hangover Liferay

Rank: New Member

Posts: 17

Join Date: May 18, 2010

Recent Posts

Juhi Kumari:
Hi,

You can define the same column name and their datatype as they defined in mapped table
and give the references of those tables.
<entity name="User_SystemUsed " local-service="true" remote-service="true">
<column name="userId" type="long" />
<column name="systemUsedId" type="long" />

<!-- References -->

<reference package-path="com.liferay.portal" entity="User" />
<reference package-path=".........." entity="SystemUsed" />
</entity>


Regards
Juhi


Thanks for your response, Here is my Service.xm file. When I try to build the services I get an exception. See below for the log.

Thanks
-M

service.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.test.t.service">
<author>test</author>
<namespace>ABC</namespace>

<entity name="SystemUsed" local-service="true" remote-service="false">
<!-- PK fields -->
<column name="systemUsedId" type="long" primary="true" />
<!-- Other fields -->
<column name="name" type="String" />

<!-- Order -->
<order by="asc">
<order-column name="name" />
</order>
<!-- Finder methods -->
<finder name="name" return-type="Collection">
<finder-column name="name" />
</finder>
</entity>

<entity name="User_SystemUsed" local-service="true" remote-service="false">

<!-- Other fields -->
<column name="userId" type="long" primary="true" />
<column name="systemUsedId" type="long" primary="true" />

<!-- References -->

<reference package-path="com.liferay.portal" entity="User" />
<reference package-path="com.test.t.service" entity="SystemUsed" />

<!-- Order -->
<order by="asc">
<order-column name="systemUsedId" />
</order>
<!-- Finder methods -->
<finder name="userId" return-type="Collection">
<finder-column name="userId" />
</finder>
<finder name="systemUsedId" return-type="Collection">
<finder-column name="systemUsedId" />
</finder>

</entity>

</service-builder>
===================================================================

Exception

com.liferay.portal.kernel.xml.DocumentException: Error on line 48 of document : The content of element type "entity" must match "(column*,order?,finder*,reference*,tx-required*)". Nested exception: The content of element type "entity" must match "(column*,order?,finder*,reference*,tx-required*)".
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:399)
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:415)
at com.liferay.portal.kernel.xml.SAXReaderUtil.read(SAXReaderUtil.java:161)
at com.liferay.portal.tools.servicebuilder.ServiceBuilder.<init>(ServiceBuilder.java:561)
at com.liferay.portal.tools.servicebuilder.ServiceBuilder.main(ServiceBuilder.java:151)
Caused by: org.dom4j.DocumentException: Error on line 48 of document : The content of element type "entity" must match "(column*,order?,finder*,reference*,tx-required*)". Nested exception: The content of element type "entity" must match "(column*,order?,finder*,reference*,tx-required*)".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.dom4j.io.SAXReader.read(SAXReader.java:365)
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:396)
... 4 more

BUILD FAILED
Juhi Kumari
RE: Liferay service.xml foreign key many-to-many
November 22, 2012 2:42 AM
Answer

Juhi Kumari

Rank: Expert

Posts: 347

Join Date: December 12, 2011

Recent Posts

Hi,
This is my service.xml
 1<service-builder package-path="com.test">
 2    <author>Juhi</author>
 3    <namespace>test</namespace>
 4   
 5    <entity name="Test" table="test" local-service="true">
 6        <column name="testId" type="long" primary="true"></column>
 7        <column name="name" type="String"></column>
 8    </entity>
 9    <entity name="TestUser" table="test_user" local-service="true">
10        <column name="testId" type="long" primary="true" entity="Test" mapping-key="testId"></column>
11        <column name="userId" type="long" primary="true"></column>
12       
13        <reference package-path="com.liferay.portal" entity="User" />
14       
15    </entity>
16   
17</service-builder>


Build service is successfull. Just check reference package-path in your service.xml once again.
Hope it will work for you.

Regards
Juhi
Jack A. Rider / Alfredo Prats
RE: Liferay service.xml foreign key many-to-many
November 22, 2012 3:46 AM
Answer

Jack A. Rider / Alfredo Prats

Rank: Regular Member

Posts: 139

Join Date: March 26, 2007

Recent Posts

Hola Hangover,

I am not sure 100% of your requirements, but if I underestand ok, you are developing a kind of master-detail table structure. May be of interest to you this example of master-detail tables , it generates also the service.xml file along with all source code and files, so you can study the generated code for your proyects.

If I am correct you are going to save a lot of time.

Saludos, Jack.
Hangover Liferay
RE: Liferay service.xml foreign key many-to-many
January 9, 2013 12:06 PM
Answer

Hangover Liferay

Rank: New Member

Posts: 17

Join Date: May 18, 2010

Recent Posts

Juhi Kumari:
Hi,
This is my service.xml
 1<service-builder package-path="com.test">
 2    <author>Juhi</author>
 3    <namespace>test</namespace>
 4   
 5    <entity name="Test" table="test" local-service="true">
 6        <column name="testId" type="long" primary="true"></column>
 7        <column name="name" type="String"></column>
 8    </entity>
 9    <entity name="TestUser" table="test_user" local-service="true">
10        <column name="testId" type="long" primary="true" entity="Test" mapping-key="testId"></column>
11        <column name="userId" type="long" primary="true"></column>
12       
13        <reference package-path="com.liferay.portal" entity="User" />
14       
15    </entity>
16   
17</service-builder>


Build service is successfull. Just check reference package-path in your service.xml once again.
Hope it will work for you.

Regards
Juhi


thanks Juhi For your response. In your service.xml you are not referring to the userId from the User entity , then what is the purpose of having it ?