Fórumok

How to define relationship with existing liferay table in service.xml

thumbnail
chris chris chris, módosítva 16 év-val korábban

How to define relationship with existing liferay table in service.xml

Regular Member Bejegyzések: 179 Csatlakozás dátuma: 2007.09.25. Legújabb bejegyzések
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

<service-builder root-dir=".." package-path="com.ext.portlet">
	<portlet name="onlineproject" short-name="onlineproject" />
	<entity name="Location" local-service="true" remote-service="false">

	<entity name="Project" local-service="true" remote-service="false" persistence-class="com.ext.portlet.onlineproject.service.persistence.ProjectPersistenceImpl2">

		<!-- PK fields -->

		<column name="projectId" type="Long" primary="true" id-type="identity" />

		<!-- Group instance -->

		<column name="groupId" type="Long" />

		<!-- Audit fields -->

		<column name="companyId" type="Long" />
		<column name="userId" type="Long" />
		<column name="userName" type="String" />
		<column name="createDate" type="Date" />
		<column name="modifiedDate" type="Date" />

		<!-- Other fields -->

		<column name="title" type="String" />
		<column name="hotjob" type="boolean" />
		<column name="descformatted" type="String" />
		<column name="desctext" type="String" />
		<column name="skilltypeId" type="Long" />
		<column name="locationId" type="Long" />
		<column name="contactId" type="Long" /> <!-- remove later -->
		<column name="startdate" type="String" />
		<column name="duration" type="String" />
		<column name="jobtypeId" type="Long" />
		<column name="paydescription" type="String" />
		
		<column name="gecoprojectId" type="Long" />
		<column name="city" type="String" />
		<column name="rmsalutation" type="String" />		
		<column name="rmfirstname" type="String" />
		<column name="rmlastname" type="String" />		
		<column name="rmemail" type="String" />		
		<column name="rmphone" type="String" />
		<column name="applicationemail" type="String" />		
		<column name="datafrom" type="Long" />
						
		<!-- relationship -->
        <column name="applications" type="Collection" entity="Application" mapping-key="projectId" />
		
        <!-- this is the problem -->
	<column name="projectLocation" type="com.liferay.portal.model.Country" entity="com.liferay.portal.model.Country" mapping-key="countryId" />
		
		<!-- finder method -->

		<finder name="locationId" return-type="Collection">
			<finder-column name="locationId" />
		</finder>
												
	</entity>

</entity></service-builder>


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

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


Then I change service.xml, like this :


<service-builder root-dir=".." package-path="com.ext.portlet">
	<portlet name="onlineproject" short-name="onlineproject" />
	<entity name="Location" local-service="true" remote-service="false">

	<entity name="Project" local-service="true" remote-service="false" persistence-class="com.ext.portlet.onlineproject.service.persistence.ProjectPersistenceImpl2">
 
       <!-- stripped -->
						
		<!-- relationship -->
        <column name="applications" type="Collection" entity="Application" mapping-key="projectId" />
		
        <!-- this is the problem -->
	<column name="projectLocation" type="Country" entity="Country" mapping-key="countryId" />
		
		<!-- finder method -->
                <!-- stripped -->	

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

</entity></service-builder>


And it gave me error like this :

build-service:
[java] 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.
[java] at org.dom4j.io.SAXReader.read(SAXReader.java:482)


Any idea ?
thumbnail
Jorge Ferrer, módosítva 16 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

Liferay Legend Bejegyzések: 2871 Csatlakozás dátuma: 2006.08.31. Legújabb bejegyzések
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.
thumbnail
Andre Kreienbring, módosítva 16 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

Regular Member Bejegyzések: 152 Csatlakozás dátuma: 2006.12.18. Legújabb bejegyzések
Hello,

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

Something like:
<column name="[MyColumn]" type="Collection" entity="com.ext.portlet.[MyCompany].[MyPortlet]" mapping-key="[MyMappingKey]" />

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

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
thumbnail
Jorge Ferrer, módosítva 16 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

Liferay Legend Bejegyzések: 2871 Csatlakozás dátuma: 2006.08.31. Legújabb bejegyzések
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:
<column name="roles" type="Collection" entity="Role" mapping-table="Groups_Roles" />
thumbnail
Andre Kreienbring, módosítva 16 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

Regular Member Bejegyzések: 152 Csatlakozás dátuma: 2006.12.18. Legújabb bejegyzések
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, módosítva 15 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

New Member Bejegyzések: 19 Csatlakozás dátuma: 2008.04.24. Legújabb bejegyzések
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.
thumbnail
Ray Augé, módosítva 15 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

Liferay Legend Bejegyzések: 1197 Csatlakozás dátuma: 2005.02.08. Legújabb bejegyzések
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:
	[snip]	
		<column name="user" type="User" entity="User" mapping-key="userId" />
		[snip]
		<reference package-path="com.liferay.portal" entity="User" />
		[snip]


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

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

public User getUser() {
	User user = null;
	if (super.getUserId() &gt; 0) {
		try {
			user = UserLocalServiceUtil.getUserById(super.getUserId());
		}
		catch (NoSuchUserException nsue) {
		}
	}
	return user;
}


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

UserExtra userextra = //... blah...
 
 User user = userextra.getUser();
Richard Gal, módosítva 15 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

New Member Bejegyzések: 19 Csatlakozás dátuma: 2008.04.24. Legújabb bejegyzések
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


...
<entity name="Game" local-service="true" remote-service="true" persistence-class="com.myco.game.service.persistence.GamePersistenceImpl">
            
...
        
        <!-- Relationships -->
        <column name="players" type="Collection" entity="com.myco.player.model.Player" mapping-table="Games_Players" />
...
</entity>


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


     [java] Building Game
     [java] 11:00:54,812 ERROR [runtime:96] 
     [java] Method public com.liferay.portal.tools.servicebuilder.Entity 
             com.liferay.portal.tools.servicebuilder.ServiceBuilder.getEntity(java.lang.String) throws java.io.IOException 
             threw an exception when invoked on com.liferay.portal.tools.servicebuilder.ServiceBuilder@d6c07
     [java] The problematic instruction:
     [java] ----------
     [java] ==&gt; assignment: tempEntity=serviceBuilder.getEntity(column.getEJBName()) [on line 120, column 33 in 
              com/liferay/portal/tools/servicebuilder/dependencies/persistence_impl.ftl]
     [java] ----------
     [java] Java backtrace for programmers:
     [java] ----------
     [java] freemarker.template.TemplateModelException: Method public com.liferay.portal.tools.servicebuilder.Entity 
              com.liferay.portal.tools.servicebuilder.ServiceBuilder.getEntity(java.lang.String) throws java.io.IOException 
              threw an exception when invoked on com.liferay.portal.tools.servicebuilder.ServiceBuilder@d6c07
     [java] 	at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:136)
     [java] Method public com.liferay.portal.tools.servicebuilder.Entity 
             com.liferay.portal.tools.servicebuilder.ServiceBuilder.getEntity(java.lang.String) throws java.io.IOException 
             threw an exception when invoked on com.liferay.portal.tools.servicebuilder.ServiceBuilder@d6c07
     [java] The problematic instruction:
     [java] ----------
     [java] ==&gt; assignment: tempEntity=serviceBuilder.getEntity(column.getEJBName()) [on line 120, column 33 in 
              com/liferay/portal/tools/servicebuilder/dependencies/persistence_impl.ftl]
     [java] 	at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
     [java] ----------
     [java] Java backtrace for programmers:
     [java] ----------
     [java] freemarker.template.TemplateModelException: Method public com.liferay.portal.tools.servicebuilder.Entity 
             com.liferay.portal.tools.servicebuilder.ServiceBuilder.getEntity(java.lang.String) throws java.io.IOException 
             threw an exception when invoked on com.liferay.portal.tools.servicebuilder.ServiceBuilder@d6c07
     [java] 	at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:136)
...


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

Thanks in advance.
Giuseppe Pantaleo, módosítva 11 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

New Member Bejegyzések: 15 Csatlakozás dátuma: 2012.06.18. Legújabb bejegyzések
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
thumbnail
David H Nebinger, módosítva 11 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

Liferay Legend Bejegyzések: 14914 Csatlakozás dátuma: 2006.09.02. Legújabb bejegyzések
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...
thumbnail
Glenn Powell, módosítva 15 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

Junior Member Bejegyzések: 43 Csatlakozás dátuma: 2008.01.08. Legújabb bejegyzések
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, módosítva 15 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

Junior Member Bejegyzések: 34 Csatlakozás dátuma: 2008.05.24. Legújabb bejegyzések
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
Christoph H., módosítva 15 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

Regular Member Bejegyzések: 147 Csatlakozás dátuma: 2007.07.31. Legújabb bejegyzések
Glenn Powell:
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.


Is it possible to get a copy of these modifications?
Before we attempt to implement the same thing it would be valuable to have a look at what you've been doing.

regards
Chris
thumbnail
auditya manikanta vadrevu, módosítva 15 év-val korábban

Problem in using service.xml

Liferay Master Bejegyzések: 621 Csatlakozás dátuma: 2008.05.06. Legújabb bejegyzések
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
package com.ext.portlet.library.service.impl;
import com.ext.portlet.library.model.Book;
import com.ext.portlet.library.service.persistence.BookUtil;
import com.ext.portlet.library.service.spring.BookLocalService;
import com.liferay.counter.service.spring.CounterServiceUtil;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
public class BookLocalServiceImpl implements BookLocalService {
	
	public Book addBook(String userId, String title) throws PortalException, SystemException {
		
		Book book = BookUtil.create(Long.toString(CounterServiceUtil.increment(Book.class.getName())));
		book.setTitle(title);
		return BookUtil.update(book);
	}
}


created file

package com.ext.portlet.library.service.impl;
import com.ext.portlet.library.model.Book;
import com.ext.portlet.library.service.persistence.BookUtil;
import com.ext.portlet.library.service.spring.BookLocalService;
import com.liferay.counter.service.spring.CounterServiceUtil;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
import com.ext.portlet.library.service.base.BookLocalServiceBaseImpl;


public class BookLocalServiceImpl extends BookLocalServiceBaseImpl {


/* i have added this code */
public Book addBook(String userId, String title) throws PortalException, SystemException {
		
		Book book = BookUtil.create(Long.toString(CounterServiceUtil.increment(Book.class.getName())));
		book.setTitle(title);
		return BookUtil.update(book);
	}


}




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 ?
thumbnail
Luke McLean, módosítva 15 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

Junior Member Bejegyzések: 46 Csatlakozás dátuma: 2007.08.16. Legújabb bejegyzések
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, módosítva 14 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

New Member Bejegyzések: 4 Csatlakozás dátuma: 2009.01.30. Legújabb bejegyzések
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, módosítva 13 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

New Member Bejegyzések: 2 Csatlakozás dátuma: 2009.05.21. Legújabb bejegyzések
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
thumbnail
Raja Nagendra Kumar, módosítva 13 év-val korábban

RE: How to define relationship with existing liferay table in service.xml

Expert Bejegyzések: 484 Csatlakozás dátuma: 2006.03.02. Legújabb bejegyzések
See this..

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

Regards,
Nagendra