论坛

主页 » Liferay Portal » English » 3. Development

组合视图 统一视图 树状图
讨论主题 [ 上一个 | 下一个 ]
toggle
Service builder - Foreign key - does it really work? Sơn Tuấn Nguyễn 2012年8月31日 上午3:20
RE: Service builder - Foreign key - does it really work? Hitesh Methani 2012年8月31日 上午4:26
RE: Service builder - Foreign key - does it really work? Hitoshi Ozawa 2012年9月1日 下午8:49
RE: Service builder - Foreign key - does it really work? Dimitrios Zigkos 2012年10月19日 下午4:01
RE: Service builder - Foreign key - does it really work? David H Nebinger 2012年10月23日 下午2:09
RE: Service builder - Foreign key - does it really work? Asmaa Ismail 2012年10月23日 下午4:35
RE: Service builder - Foreign key - does it really work? David H Nebinger 2012年10月24日 上午8:07
RE: Service builder - Foreign key - does it really work? Jack Bakker 2012年10月24日 上午8:30
RE: Service builder - Foreign key - does it really work? David H Nebinger 2012年10月24日 上午8:47
RE: Service builder - Foreign key - does it really work? Jack Bakker 2012年10月24日 下午4:10
RE: Service builder - Foreign key - does it really work? David H Nebinger 2012年10月25日 上午6:40
RE: Service builder - Foreign key - does it really work? Jack Bakker 2012年10月26日 下午5:22
RE: Service builder - Foreign key - does it really work? David H Nebinger 2012年10月27日 下午1:28
RE: Service builder - Foreign key - does it really work? Jack Bakker 2012年10月28日 上午9:37
RE: Service builder - Foreign key - does it really work? Dimitrios Zigkos 2012年10月24日 下午1:31
Sơn Tuấn Nguyễn
Service builder - Foreign key - does it really work?
2012年8月31日 上午3:20
答复

Sơn Tuấn Nguyễn

等级: Junior Member

帖子: 37

加入日期: 2010年3月1日

最近的帖子

As i've developed for months now with MCV Portlet, service builder is cool.
And I know how to use foreign key like this:

<column name="locationId" type="Collection" entity="Location" mapping-key="id" />
<column name="locationId" type="String" />

However, yesterday I encountered a case that I could insert a record with locationId doesn't exist in parent table Location.

So the FK referenced in service.xml only helps if you update data in parent table, it will update data in child table.
And it doesn't check if inserted data in child table should exist in parent table, does it?

If this is true, we have to manually check for valid data right ?

Thanks in advance
Hitesh Methani
RE: Service builder - Foreign key - does it really work?
2012年8月31日 上午4:26
答复

Hitesh Methani

等级: Regular Member

帖子: 169

加入日期: 2010年6月24日

最近的帖子

Hello Sơn Tuấn Nguyễn,

Even we encountered the same problem.
Think you need to have a manual check in that case.

Regards,
Hitesh
Hitoshi Ozawa
RE: Service builder - Foreign key - does it really work?
2012年9月1日 下午8:49
答复

Hitoshi Ozawa

等级: Liferay Legend

帖子: 7952

加入日期: 2010年3月23日

最近的帖子

If you check the database, there really isn't any FK created. service.xml just defines specifies "relationships". If you studied service modelling in service oriented architecture, this is how it is suppose to be to make service autonomous.
Dimitrios Zigkos
RE: Service builder - Foreign key - does it really work?
2012年10月19日 下午4:01
答复

Dimitrios Zigkos

等级: New Member

帖子: 6

加入日期: 2012年10月19日

最近的帖子

Hello Hitoshi and all,
I try to handle the one-to-many relationship in Liferay. one of my problems is that there are no setters for the Collections in the case of FK. I look up the PersistenceImpl.class service builder creates, and I find lists of queries that refer to FKs. But those queries throw ERRORS when I run the portlet!

Lets say that we have the following entities in the service.xml (A persons has many jobs to do):
<entity name="Person" table="Person" local-service="true" remote-service="false">
<column name="personID" type="long" primary="true" />
<column name="firstName" type="String" />
<column name="lastName" type="String" />
<column name="jobs" type="Collection" entity="Job" mapping-key="jobID" />
...
</entity>
<entity name="Job" table="Job" local-service="true" remote-service="false">
<column name="jobID" type="long" primary="true" />
<column name="title" type="String" />
<column name="description" type="String" />
...
</entity>

Am I correct to consider that inside PersonLocalServiceImpl.java I can call the PersonUtil.getJobs(personID) for getting the List<Jobs> --(getter)?
If yes, how can I set the collection I have prepared elsewhere into this Person instance --(setter)?

Please assist...
Thank you,
Dimitrios Zigkos
David H Nebinger
RE: Service builder - Foreign key - does it really work?
2012年10月23日 下午2:09
答复

David H Nebinger

Community Moderator

等级: Liferay Legend

帖子: 8433

加入日期: 2006年9月1日

最近的帖子

Liferay's Service Builder does not support 1-many or fkey relationships.

It is up to you to implement this.
Asmaa Ismail
RE: Service builder - Foreign key - does it really work?
2012年10月23日 下午4:35
答复

Asmaa Ismail

等级: New Member

帖子: 10

加入日期: 2012年9月22日

最近的帖子

I have a related Question here..
If I've a parent child case
Do I have to insert the parent alone and then loop on the child list and insert each object individually ??
Or how can I make the parent object persists its children with it??
Thanks in Advance,
Best Regards,
Asmaa
David H Nebinger
RE: Service builder - Foreign key - does it really work?
2012年10月24日 上午8:07
答复

David H Nebinger

Community Moderator

等级: Liferay Legend

帖子: 8433

加入日期: 2006年9月1日

最近的帖子

You persist them all (parent and children) individually.
Jack Bakker
RE: Service builder - Foreign key - does it really work?
2012年10月24日 上午8:30
答复

Jack Bakker

等级: Liferay Master

帖子: 734

加入日期: 2010年1月3日

最近的帖子

how do u manage transactions and rollback David ?
David H Nebinger
RE: Service builder - Foreign key - does it really work?
2012年10月24日 上午8:47
答复

David H Nebinger

Community Moderator

等级: Liferay Legend

帖子: 8433

加入日期: 2006年9月1日

最近的帖子

A transaction will be automatically wrapped around every service builder method that starts with add, update, delete (and I believe create and some others, but don't have a list handy).

To bind all of these into a single transaction, the key is to create one entry point in your XxxLocalServiceImpl class to handle the bulk insert/update:

 1public Parent addParentAndChildren(final Parent parent, final List<Child> children) {
 2  // persist the parent object the way you normally would
 3
 4  // handle the children here, whether you do a simple case of deleting the current children and blindly adding the children in the list
 5  // or a more complex method of selectively adding, removing, and updating children as given in the list.
 6
 7  // return the parent instance, this is standard for all add/update operations of Service Builder
 8  return parent;
 9}


Now when you call ParentLocalServiceUtil.addParentAndChildren(myParent, myChildren), this will all be handled within the scope of a single transaction.

So my original statement of "parent and children must be inserted manually" is still true, it is still up to the developer to manage the inserts.
Dimitrios Zigkos
RE: Service builder - Foreign key - does it really work?
2012年10月24日 下午1:31
答复

Dimitrios Zigkos

等级: New Member

帖子: 6

加入日期: 2012年10月19日

最近的帖子

I've finally resolved the problem with the FK.
I had to change the Collection of the Person entity as follows:
<column name="jobs" type="Collection" entity="Job" mapping-key="personID" />
and in the Job entity to insert a new column:
<column name="personID" type="long" />

This will allow the invocation of both:
PersonUtil.getJobs(personID)
PersonUtil.setJobs(List<Job>)

So, without the declaration of a FK, Liferay handles implicitly the personID coloumn of Job as a real FK.
It will justify the existence of that column in the queries in the class PersonPersistenceImpl.

I hope this helps.
Thank you all for the contribution.
Jack Bakker
RE: Service builder - Foreign key - does it really work?
2012年10月24日 下午4:10
答复

Jack Bakker

等级: Liferay Master

帖子: 734

加入日期: 2010年1月3日

最近的帖子

David, inside your addParentAndChildren, could you safely call other XxxLocalServiceUtil.add(Xxx xxx) or better to use xxxPersistence methods to do your add, update, delete
David H Nebinger
RE: Service builder - Foreign key - does it really work?
2012年10月25日 上午6:40
答复

David H Nebinger

Community Moderator

等级: Liferay Legend

帖子: 8433

加入日期: 2006年9月1日

最近的帖子

You can safely call either. Since the transaction is already open, a new transaction will not be created for the internal calls. Transactions just get created when there is not already one in play...
Jack Bakker
RE: Service builder - Foreign key - does it really work?
2012年10月26日 下午5:22
答复

Jack Bakker

等级: Liferay Master

帖子: 734

加入日期: 2010年1月3日

最近的帖子

David H Nebinger:
You can safely call either. Since the transaction is already open, a new transaction will not be created for the internal calls. Transactions just get created when there is not already one in play...


Thanks David ; does this hold true across multiple service.xml with different tx-manager ?

I still have to dig into the belly of this stuff ; hoping u have more experience
David H Nebinger
RE: Service builder - Foreign key - does it really work?
2012年10月27日 下午1:28
答复

David H Nebinger

Community Moderator

等级: Liferay Legend

帖子: 8433

加入日期: 2006年9月1日

最近的帖子

It should. The txn is created for the first method and the others where a txn would have been created (via AOP) will see there is already an open transaction and not create a new one...
Jack Bakker
RE: Service builder - Foreign key - does it really work?
2012年10月28日 上午9:37
答复

Jack Bakker

等级: Liferay Master

帖子: 734

加入日期: 2010年1月3日

最近的帖子

Thanks David