Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
How to define relationship with existing liferay table in service.xml Christianto Sahat February 13, 2008 11:11 PM
RE: How to define relationship with existing liferay table in service.xml Jorge Ferrer February 24, 2008 6:09 AM
RE: How to define relationship with existing liferay table in service.xml Andre Kreienbring February 25, 2008 8:00 AM
RE: How to define relationship with existing liferay table in service.xml Jorge Ferrer February 27, 2008 12:10 PM
RE: How to define relationship with existing liferay table in service.xml Andre Kreienbring March 7, 2008 2:00 AM
RE: How to define relationship with existing liferay table in service.xml Richard Gal May 8, 2008 4:06 AM
RE: How to define relationship with existing liferay table in service.xml Ray Augé May 8, 2008 8:37 AM
RE: How to define relationship with existing liferay table in service.xml Richard Gal May 14, 2008 2:05 AM
RE: How to define relationship with existing liferay table in service.xml Giuseppe Pantaleo June 29, 2012 7:20 AM
RE: How to define relationship with existing liferay table in service.xml David H Nebinger June 29, 2012 11:10 AM
RE: How to define relationship with existing liferay table in service.xml Glenn Powell July 8, 2008 2:30 PM
RE: How to define relationship with existing liferay table in service.xml Alf Høgemark August 29, 2008 5:58 AM
Problem in using service.xml Auditya manikanta Vadrevu August 31, 2008 11:43 PM
RE: How to define relationship with existing liferay table in service.xml Luke McLean February 10, 2009 4:49 PM
RE: How to define relationship with existing liferay table in service.xml Smily M May 4, 2009 11:53 PM
RE: How to define relationship with existing liferay table in service.xml CHARFI Sofien March 16, 2011 4:53 PM
RE: How to define relationship with existing liferay table in service.xml Raja Nagendra Kumar March 16, 2011 7:23 PM
Christianto Sahat
How to define relationship with existing liferay table in service.xml
February 13, 2008 11:11 PM
Answer

Christianto Sahat

Rank: Regular Member

Posts: 176

Join Date: September 24, 2007

Recent Posts

Hi all,

I have a table that has many to one relationship with existing Liferay table, that is country table.
How to define relation among them in service.xml ( I use Liferay 4.3.3 ) ?

This is how I define in service.xml
 1
 2<service-builder root-dir=".." package-path="com.ext.portlet">
 3    <portlet name="onlineproject" short-name="onlineproject" />
 4    <entity name="Location" local-service="true" remote-service="false">
 5
 6    <entity name="Project" local-service="true" remote-service="false" persistence-class="com.ext.portlet.onlineproject.service.persistence.ProjectPersistenceImpl2">
 7
 8        <!-- PK fields -->
 9
10        <column name="projectId" type="Long" primary="true" id-type="identity" />
11
12        <!-- Group instance -->
13
14        <column name="groupId" type="Long" />
15
16        <!-- Audit fields -->
17
18        <column name="companyId" type="Long" />
19        <column name="userId" type="Long" />
20        <column name="userName" type="String" />
21        <column name="createDate" type="Date" />
22        <column name="modifiedDate" type="Date" />
23
24        <!-- Other fields -->
25
26        <column name="title" type="String" />
27        <column name="hotjob" type="boolean" />
28        <column name="descformatted" type="String" />
29        <column name="desctext" type="String" />
30        <column name="skilltypeId" type="Long" />
31        <column name="locationId" type="Long" />
32        <column name="contactId" type="Long" /> <!-- remove later -->
33        <column name="startdate" type="String" />
34        <column name="duration" type="String" />
35        <column name="jobtypeId" type="Long" />
36        <column name="paydescription" type="String" />
37       
38        <column name="gecoprojectId" type="Long" />
39        <column name="city" type="String" />
40        <column name="rmsalutation" type="String" />       
41        <column name="rmfirstname" type="String" />
42        <column name="rmlastname" type="String" />       
43        <column name="rmemail" type="String" />       
44        <column name="rmphone" type="String" />
45        <column name="applicationemail" type="String" />       
46        <column name="datafrom" type="Long" />
47                        
48        <!-- relationship -->
49        <column
50            name="applications"
51            type="Collection"
52            entity="Application"
53            mapping-key="projectId"
54        />
55       
56        <!-- this is the problem -->
57    <column
58        name="projectLocation"
59        type="com.liferay.portal.model.Country"
60        entity="com.liferay.portal.model.Country"
61        mapping-key="countryId"
62    />
63       
64        <!-- finder method -->
65
66        <finder name="locationId" return-type="Collection">
67            <finder-column name="locationId" />
68        </finder>
69                                                
70    </entity>


If I define like above, it's always gave me error when I do a 'build-service'. The error is like this :

Building Project
14:57:49,093 ERROR [java:183] PARSER_ERROR
D:\chris\training\liferay4.3.3\ext\ext-impl\ServiceBuilder.temp:1:1608: unexpected token: null
at de.hunsicker.jalopy.language.JavaParser.postfixExpression(JavaParser.java:5550)


Then I change service.xml, like this :

 1
 2<service-builder root-dir=".." package-path="com.ext.portlet">
 3    <portlet name="onlineproject" short-name="onlineproject" />
 4    <entity name="Location" local-service="true" remote-service="false">
 5
 6    <entity name="Project" local-service="true" remote-service="false" persistence-class="com.ext.portlet.onlineproject.service.persistence.ProjectPersistenceImpl2">
 7
 8       <!-- stripped -->
 9                        
10        <!-- relationship -->
11        <column
12            name="applications"
13            type="Collection"
14            entity="Application"
15            mapping-key="projectId"
16        />
17       
18        <!-- this is the problem -->
19    <column
20        name="projectLocation"
21        type="Country"
22        entity="Country"
23        mapping-key="countryId"
24    />
25       
26        <!-- finder method -->
27                <!-- stripped -->   
28
29   
30           <reference package-path="com.liferay.portal" entity="Country"/>
31                                       
32    </entity>


And it gave me error like this :

build-service:
org.dom4j.DocumentException: Error on line 248 of document file:///D:/chris/training/liferay4.3.3/ext/ext-impl/service.xml : Element type "reference" must be declared. Nested exception: Element type "reference" must be declared.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)


Any idea ?
Jorge Ferrer
RE: How to define relationship with existing liferay table in service.xml
February 24, 2008 6:09 AM
Answer

Jorge Ferrer

LIFERAY STAFF

Rank: Liferay Legend

Posts: 2768

Join Date: August 31, 2006

Recent Posts

Hi Chris,

I'm not a 100% sure of this, but I think Service Builder only allows defining relationships between entities that are defined in the same service.xml file.
Andre Kreienbring
RE: How to define relationship with existing liferay table in service.xml
February 25, 2008 8:00 AM
Answer

Andre Kreienbring

Rank: Junior Member

Posts: 82

Join Date: December 18, 2006

Recent Posts

Hello,

If I may correct Mr. Jorge (Yoda) Ferrer emoticon

Something like:
<column name="" type="Collection" entity="com.ext.portlet.." mapping-key="" />

should define a relationship to the entity defined in the service.xml that is stored in
\ext-impl\src\com\ext\portlet\\

Feel free to read this: The service.xml DTD

By the way: If someone could explain me how this mechanism works I would be very thankful.

Suppose you have an ext service like UserExtra with an userId column in it. To define a many-to-one (one-to-one) relationship to the portal user model I would write:

<entity name="UserExtra" local-service="true">
<column name="userExtraId" type="long" primary="true" />
<column name="someExtraProperty" type="String" />
<column name="userId" type="long" />
<column name="User" type="Collection" entity="com.liferay.portal.User" mapping-key="userId" />
</entity>

But this this creates not the expected code emoticon . Something like .getUser()...

Maybe "Collection" is not the right type to use? I'm confused emoticon
Jorge Ferrer
RE: How to define relationship with existing liferay table in service.xml
February 27, 2008 12:10 PM
Answer

Jorge Ferrer

LIFERAY STAFF

Rank: Liferay Legend

Posts: 2768

Join Date: August 31, 2006

Recent Posts

Hi André,

Thanks for correcting me. Yoda sometimes makes mistakes on purpose to find new Jedi candidates emoticon

Regarding your question, for many to many relationships you have to define a mapping table as in:
1<column
2    name="roles"
3    type="Collection"
4    entity="Role"
5    mapping-table="Groups_Roles"
6/>
Andre Kreienbring
RE: How to define relationship with existing liferay table in service.xml
March 7, 2008 2:00 AM
Answer

Andre Kreienbring

Rank: Junior Member

Posts: 82

Join Date: December 18, 2006

Recent Posts

Absolutely!

But in my example I have a one-to-one relationship... One UserExtra entry is referencing one User entry by the userId.

And I just can't convince Service Builder to generate the Code to do something like:

UserExtra userextra = //... blah...

User = userextra.getUser();
Richard Gal
RE: How to define relationship with existing liferay table in service.xml
May 8, 2008 4:06 AM
Answer

Richard Gal

Rank: New Member

Posts: 19

Join Date: April 24, 2008

Recent Posts

Andre Kreienbring:
Absolutely!

But in my example I have a one-to-one relationship... One UserExtra entry is referencing one User entry by the userId.

And I just can't convince Service Builder to generate the Code to do something like:

UserExtra userextra = //... blah...

User = userextra.getUser();


Hi Andre

I am facing the same problem. Have you managed to solve this one-to-one issue?

Thanks for your help in advance,
Richie.
Ray Augé
RE: How to define relationship with existing liferay table in service.xml
May 8, 2008 8:37 AM
Answer

Ray Augé

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1195

Join Date: February 7, 2005

Recent Posts

Yeah, it's currently not working like I'd expect it. It seems there is a bug with creating one-to-one associations in ServiceBuilder.

ie. I tried the following:
1    [snip]   
2        <column name="user" type="User" entity="User" mapping-key="userId" />
3        [snip]
4        <reference package-path="com.liferay.portal" entity="User" />
5        [snip]


but I get a failure during the build... seems that the column doesn't work when not a Collection type in an association.

SO, for now you have to do it the old fashioned way which is to crack open your UserExtraImpl.java class, and implement this method:

 1public User getUser() {
 2    User user = null;
 3    if (super.getUserId() > 0) {
 4        try {
 5            user = UserLocalServiceUtil.getUserById(super.getUserId());
 6        }
 7        catch (NoSuchUserException nsue) {
 8        }
 9    }
10    return user;
11}


then regen your service. Then you should be good to make your:

1UserExtra userextra = //... blah...
2
3 User user = userextra.getUser();
Richard Gal
RE: How to define relationship with existing liferay table in service.xml
May 14, 2008 2:05 AM
Answer

Richard Gal

Rank: New Member

Posts: 19

Join Date: April 24, 2008

Recent Posts

Hi

Back to the original question of this thread: Is it possible to define a many-to-many relationship between 2 entities defined in 2 separate service.xml's?

More specifically, let's suppose that

I have one service.xml under ext-impl/src/com/myco/game with an entity Game and
I have the other service.xml under ext-impl/src/com/myco/player with the entity Player

and one player can participate in multiple games and one game can be played by multiple players. I would like to be able to query all the players playing a game and have the following relationship in ext-impl/src/com/myco/game/service.xml

 1
 2...
 3<entity name="Game"
 4            local-service="true"
 5            remote-service="true"
 6            persistence-class="com.myco.game.service.persistence.GamePersistenceImpl">
 7            
 8...
 9       
10        <!-- Relationships -->
11        <column name="players" type="Collection"
12                     entity="com.myco.player.model.Player" mapping-table="Games_Players" />
13...


Running ServiceBuilder on this service.xml fails with the following error:

 1
 2     [java] Building Game
 3     [java] 11:00:54,812 ERROR [runtime:96]
 4     [java] Method public com.liferay.portal.tools.servicebuilder.Entity
 5             com.liferay.portal.tools.servicebuilder.ServiceBuilder.getEntity(java.lang.String) throws java.io.IOException
 6             threw an exception when invoked on com.liferay.portal.tools.servicebuilder.ServiceBuilder@d6c07
 7     [java] The problematic instruction:
 8     [java] ----------
 9     [java] ==> assignment: tempEntity=serviceBuilder.getEntity(column.getEJBName()) [on line 120, column 33 in
10              com/liferay/portal/tools/servicebuilder/dependencies/persistence_impl.ftl]
11     [java] ----------
12     [java] Java backtrace for programmers:
13     [java] ----------
14     [java] freemarker.template.TemplateModelException: Method public com.liferay.portal.tools.servicebuilder.Entity
15              com.liferay.portal.tools.servicebuilder.ServiceBuilder.getEntity(java.lang.String) throws java.io.IOException
16              threw an exception when invoked on com.liferay.portal.tools.servicebuilder.ServiceBuilder@d6c07
17     [java]     at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:136)
18     [java] Method public com.liferay.portal.tools.servicebuilder.Entity
19             com.liferay.portal.tools.servicebuilder.ServiceBuilder.getEntity(java.lang.String) throws java.io.IOException
20             threw an exception when invoked on com.liferay.portal.tools.servicebuilder.ServiceBuilder@d6c07
21     [java] The problematic instruction:
22     [java] ----------
23     [java] ==> assignment: tempEntity=serviceBuilder.getEntity(column.getEJBName()) [on line 120, column 33 in
24              com/liferay/portal/tools/servicebuilder/dependencies/persistence_impl.ftl]
25     [java]     at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
26     [java] ----------
27     [java] Java backtrace for programmers:
28     [java] ----------
29     [java] freemarker.template.TemplateModelException: Method public com.liferay.portal.tools.servicebuilder.Entity
30             com.liferay.portal.tools.servicebuilder.ServiceBuilder.getEntity(java.lang.String) throws java.io.IOException
31             threw an exception when invoked on com.liferay.portal.tools.servicebuilder.ServiceBuilder@d6c07
32     [java]     at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:136)
33...


How can I achieve that ServiceBuilder processes ext-impl/src/com/myco/player/service.xml and notices Player?

Thanks in advance.
Glenn Powell
RE: How to define relationship with existing liferay table in service.xml
July 8, 2008 2:30 PM
Answer

Glenn Powell

Rank: Junior Member

Posts: 44

Join Date: January 8, 2008

Recent Posts

Hello Ray,

Has a bug been filed for this? I think allowing for one-to-one and many-to-one columns is fairly important.
I have been tinkering with ServiceBuilder quite a bit, and I have fixed this problem on my local copy. And in addition added the capability to generate proper HBM tags for these mappings in the ext_hbm.xml, so that Hibernate can properly recognize the associations. This is extremely invaluable if you are using the dynamicQuery() functionality (which I am).

My custom ServiceBuilder also auto-generates fully-implemented *ServiceImpl classes using the <finder> definitions in the service.xml. This may not be necessary for everyone, but I found that I wasted many hours simply coding in the monotonous implementation methods which simply call all the Persistence methods.

There are a few other minor enhancements as well, but I'll document them later.

I'll post my changes to JIRA at some point, but I wanted to keep this thread going to see if you had any other comments related to these mapping issues or just to increasing ServiceBuilder's functionality.
Alf Høgemark
RE: How to define relationship with existing liferay table in service.xml
August 29, 2008 5:58 AM
Answer

Alf Høgemark

Rank: Junior Member

Posts: 34

Join Date: May 24, 2008

Recent Posts

I definitely agree with :
"
Has a bug been filed for this? I think allowing for one-to-one and many-to-one columns is fairly important.
"


"
I have been tinkering with ServiceBuilder quite a bit, and I have fixed this problem on my local copy. And in addition added the capability to generate proper HBM tags for these mappings in the ext_hbm.xml, so that Hibernate can properly recognize the associations. This is extremely invaluable if you are using the dynamicQuery() functionality (which I am).
"

It would be great if you could shre these ServiceBuilder modifications, it is certainly something which should be integrated into the standard ServiceBuilder.

I need the same ServiceBuilder capabilties in a project, and it would be good to not waste time improving the ServiceBuilder in the same manner as you have, and instead being able to spend the effort on making the ServiceBuilder even bettter than your version.

Regards
Alf Hogemark
Auditya manikanta Vadrevu
Problem in using service.xml
August 31, 2008 11:43 PM
Answer

Auditya manikanta Vadrevu

Rank: Liferay Master

Posts: 621

Join Date: May 6, 2008

Recent Posts

hi ray,

i tried to develop struts portlet with database interaction based upon the liferay ppt which i have attached. I have not success fully deployed due to some difference in the ppt and the actually created files

i have written the service.xml as specified in the ppt for two columns in Book table. then executed ant build-service. Only two folders have created model and service.

Difference i have observed :



1. In BookLocalServiceImpl.java, the created file exends BookLocalServiceBaseImpl and given file in ppt implements BookLocalService

given file
 1package com.ext.portlet.library.service.impl;
 2import com.ext.portlet.library.model.Book;
 3import com.ext.portlet.library.service.persistence.BookUtil;
 4import com.ext.portlet.library.service.spring.BookLocalService;
 5import com.liferay.counter.service.spring.CounterServiceUtil;
 6import com.liferay.portal.PortalException;
 7import com.liferay.portal.SystemException;
 8public class BookLocalServiceImpl implements BookLocalService {
 9   
10    public Book addBook(String userId, String title) throws PortalException, SystemException {
11       
12        Book book = BookUtil.create(Long.toString(CounterServiceUtil.increment(Book.class.getName())));
13        book.setTitle(title);
14        return BookUtil.update(book);
15    }
16}


created file
 1
 2package com.ext.portlet.library.service.impl;
 3import com.ext.portlet.library.model.Book;
 4import com.ext.portlet.library.service.persistence.BookUtil;
 5import com.ext.portlet.library.service.spring.BookLocalService;
 6import com.liferay.counter.service.spring.CounterServiceUtil;
 7import com.liferay.portal.PortalException;
 8import com.liferay.portal.SystemException;
 9import com.ext.portlet.library.service.base.BookLocalServiceBaseImpl;
10
11
12public class BookLocalServiceImpl extends BookLocalServiceBaseImpl {
13
14
15/* i have added this code */
16public Book addBook(String userId, String title) throws PortalException, SystemException {
17       
18        Book book = BookUtil.create(Long.toString(CounterServiceUtil.increment(Book.class.getName())));
19        book.setTitle(title);
20        return BookUtil.update(book);
21    }
22
23
24}




2. The ppt suggests to add two statement in AddbookAction.java but neither the folder action nor the file is created .
then i only created the folder action and file and then made the entries.

3. finally while executing ant deploy

it has thrown following errors

import com.ext.portlet.library.service.spring.BookLocalService;
import com.liferay.counter.service.spring.CounterServiceUtil;
import com.ext.portlet.library.service.spring.BookLocalServiceUtil;

these above packages are not found



what could be the problem ?
Attachments: Connecting to Liferay Database.ppt (353.5k)
Luke McLean
RE: How to define relationship with existing liferay table in service.xml
February 10, 2009 4:49 PM
Answer

Luke McLean

Rank: Junior Member

Posts: 46

Join Date: August 15, 2007

Recent Posts

Hi Ray, Could you please advise whether this bug was ever logged? If it was could you please advise the reference. I would like to see whether the newest version has it fixed... I'm using 5.1.1 and it appears to still be an issue.

Thanks
Smily M
RE: How to define relationship with existing liferay table in service.xml
May 4, 2009 11:53 PM
Answer

Smily M

Rank: New Member

Posts: 4

Join Date: January 30, 2009

Recent Posts

Hi,


Its been quiet a while that we have been using & understanding liferay portal, and its been worthwhile & fantastic.

But i am not understanding one thing, Why is that we cannot assign a foreign key in service-builder???Why is there nothing called foreign key in complete liferay??? ( please correct me if i am wrong ) cz foreign key is something very crucial... its just not clear as to how to achieve it.

And reading through forums i have seen that its suggested to handle anything reg foreign key through our code, how do we do that exactly? Any samples or any doc on it ?

Any sample portlet on how can we use one-to-one & many-to-many relationships?


Kindly suggest me with the above emoticon

Thanks in advance.
Regards !
CHARFI Sofien
RE: How to define relationship with existing liferay table in service.xml
March 16, 2011 4:53 PM
Answer

CHARFI Sofien

Rank: New Member

Posts: 2

Join Date: May 20, 2009

Recent Posts

Hello Ray
I m working with liferay 6.0.5 + sdk
How can make a relationship with a new table in service.xml (2 entity defined in service.xml)
Thanks

Sofien
Raja Nagendra Kumar
RE: How to define relationship with existing liferay table in service.xml
March 16, 2011 7:23 PM
Answer

Raja Nagendra Kumar

Rank: Expert

Posts: 484

Join Date: March 1, 2006

Recent Posts

See this..

http://www.liferay.com/community/forums/-/message_boards/message/1061266

Regards,
Nagendra
Giuseppe Pantaleo
RE: How to define relationship with existing liferay table in service.xml
June 29, 2012 7:20 AM
Answer

Giuseppe Pantaleo

Rank: New Member

Posts: 15

Join Date: June 18, 2012

Recent Posts

Hi,
I'm resuming this post because I'm experiencing the problem with LR6.1;

Richard Gal:
Hi

Back to the original question of this thread: Is it possible to define a many-to-many relationship between 2 entities defined in 2 separate service.xml's?


Has anyone an answer to that question?

Tnx in advance
David H Nebinger
RE: How to define relationship with existing liferay table in service.xml
June 29, 2012 11:10 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11087

Join Date: September 1, 2006

Recent Posts

Giuseppe Pantaleo:
Hi,
I'm resuming this post because I'm experiencing the problem with LR6.1;

Richard Gal:
Hi

Back to the original question of this thread: Is it possible to define a many-to-many relationship between 2 entities defined in 2 separate service.xml's?


Has anyone an answer to that question?

Tnx in advance


Liferay doesn't support foreign keys in any of their tables, nor do they support it in SB.

Liferay doesn't use many to many relationships directly (for example, the UserGroupRole maps to a table that has what we would normally call foreign keys to the User, Group, and Role tables). Nor do they support it in SB.

Liferay manually handles the many to many relationships, and you are left to do the same.

By following their lead, you do not have to worry about entities in separate service.xmls. Service.xml A may have a Student entity defined w/ a studentId as the primary, and Service.xml B can have the Course entity with the courseId as the primary, and a corresponding CourseStudent entity which has a studentId and courseId defined as it's primary key.

P.S. even though you have the same issue as a seriously old thread, you really should start a new thread rather than trying to bring an old one back to life. The issues and concerns in the old threads may have nothing to do with the 6.1 version of Liferay, and answers for this version may not apply to the older version this thread was started for...