Forums de discussion

One to Many relationship with liferay table using ServiceBuilder

thumbnail
Soori Babu Meesala, modifié il y a 8 années.

One to Many relationship with liferay table using ServiceBuilder

Junior Member Publications: 43 Date d'inscription: 10/07/14 Publications récentes
Hi,
I need to make one to many relationship with my custom table with liferay user table. How can I achieve this using Liferay Service Builder.
I tried below code for course---user (oneTomany) Relation


<entity name="Course" local-service="true" remote-service="true">
 <column name="courseId" type="long" primary="true" />
<column name="courseName" type="String" />

<!--   Foreign Key-->

  <column name="users" type="Collection" entity="User_" />

</entity>


But I am unable to map my custom table with Liferay User table. Any Suggestions?

~Regards,
Soori Babu Meesala
thumbnail
Jan Geißler, modifié il y a 8 années.

RE: One to Many relationship with liferay table using ServiceBuilder

Liferay Master Publications: 735 Date d'inscription: 05/07/11 Publications récentes
Is your relation really an 1-n relation or an n-m? A user can have Multiple Courses and a Course has multiple Users, right? That this would be an n-m relation, right?
If not, in which direction is your relation? User to Courses or vice versa?
thumbnail
Soori Babu Meesala, modifié il y a 8 années.

RE: One to Many relationship with liferay table using ServiceBuilder

Junior Member Publications: 43 Date d'inscription: 10/07/14 Publications récentes
Hi,
It is only one-to-many. One course has many users. Many users can register into one course. ie. one user can register in only one course.

~Regards,
Soori Babu Meesala
thumbnail
Jan Geißler, modifié il y a 8 années.

RE: One to Many relationship with liferay table using ServiceBuilder

Liferay Master Publications: 735 Date d'inscription: 05/07/11 Publications récentes
Soori Babu Meesala:
Hi,
It is only one-to-many. One course has many users. Many users can register into one course. ie. one user can register in only one course.


If this is REALLY what you want (and I doubt that) than you could resolve this quite easily via a Custom Field on the User Object. That would even free you from writing an own Service Layer for that. So you create a CustomField where you store your courseId.
I really want to point out here at this point, that I would implement an n-m approach, as I am quite sure there WILL be the need to store more than one course per User.
thumbnail
Soori Babu Meesala, modifié il y a 8 années.

RE: One to Many relationship with liferay table using ServiceBuilder

Junior Member Publications: 43 Date d'inscription: 10/07/14 Publications récentes
Hi as per my requirement I should not go with Custom Fields. I just need to convert the below pojo into entity.
Here is my Course Pojo


public class Course
{
  private long courseId;
  private String courseName;
  private List<long> enrolledUserIds;
 -- getter and setter methods---

}
</long>


How can I write this as a entity in service.xml file?
thumbnail
Soori Babu Meesala, modifié il y a 8 années.

RE: One to Many relationship with liferay table using ServiceBuilder

Junior Member Publications: 43 Date d'inscription: 10/07/14 Publications récentes
I tried below line for adding enrolled user ids.

 <column name="enrolledusers" type="Collection" entity="User_"></column>


But the column "enrolledusers" is not getting created.
thumbnail
Vishal Kumar, modifié il y a 8 années.

RE: One to Many relationship with liferay table using ServiceBuilder

Regular Member Publications: 198 Date d'inscription: 12/12/12 Publications récentes
But the column "enrolledusers" is not getting created.


In which table you are seeing?
thumbnail
Soori Babu Meesala, modifié il y a 8 années.

RE: One to Many relationship with liferay table using ServiceBuilder

Junior Member Publications: 43 Date d'inscription: 10/07/14 Publications récentes
Vishal Kumar:
But the column "enrolledusers" is not getting created.


In which table you are seeing?


course table.
thumbnail
Jan Geißler, modifié il y a 8 années.

RE: One to Many relationship with liferay table using ServiceBuilder

Liferay Master Publications: 735 Date d'inscription: 05/07/11 Publications récentes
What you are trying do to would only be possible, if you check out the portal, adjust service.xml for the user, rebuild the service of the portal, and build the portal itself. I would discourage you in doing so. Even if it adds some complexity not needed, I would go for the n-m approach.

Do a mapping table For Users_Courses. If you want to make sure, that one user can only have one course you should add an entry in the service builder like this:
	
		<finder name="User_Course" return-type="UserCourse" unique="true">
			<finder-column name="companyId" />
			<finder-column name="courseId" />
			<finder-column name="userId" />
		</finder>


By doing that, you are creating an Index which will be unique for those three properties, so you have an 1-n relation in a n-m database schema ;)
thumbnail
Jack Bakker, modifié il y a 8 années.

RE: One to Many relationship with liferay table using ServiceBuilder

Liferay Master Publications: 978 Date d'inscription: 03/01/10 Publications récentes
I've been thinking on the semantics of one-to-many compared to many-to-many from primarily a subjective visual parallax perspective while infusing auditory parallax which, for difference between, involves speed of light vs. sound difference between source emittance and my own sense of reality (albeit wrt to only visual and auditory, burp). Predictive formulation for such is within variable constraints of a four-dimensional time/space - where expectations for such is towards validating hypotheses according to both traditional scientific method (many to many ; objectively true?) experiments and action science (one to many; subjectively true?) experiments .

I fully expect my above is subject also to TL;DR and thread hijacking both via individual perspective (1:n) and collective agreement (1:n -> n:m). (not to mention WTF which to some means working for the future)

Soori, you might look at the following and extend the approach:
https://www.liferay.com/community/forums/-/message_boards/view_message/45927151#_19_message_45927151
thumbnail
David H Nebinger, modifié il y a 8 années.

RE: One to Many relationship with liferay table using ServiceBuilder

Liferay Legend Publications: 14917 Date d'inscription: 02/09/06 Publications récentes
If it were one to many, your column would be simply:

<column name="userId" type="long" />


That means that you'll have multiple Course records in the database, each pointing to the user that is taking the course.

If, as Jan points out, you want a many to many table, you need the separate joining entity:

<entity name="CourseStudent" local-service="true">
  <column name="courseId" type="long" primary="true" />
  <column name="userId" type="long" primary="true" />
  <finder name="Course" return-type="Collection">
    <finder-column name="courseId" />
  </finder>
  <finder name="User" return-type="Collection">
    <finder-column name="userId" />
  </finder>
</entity>


In either case, User is an external entity type outside of your entity so you may have trouble with class loader issues and referencing the objects. Although it may suck doing multiple retrieves, sometimes not having to worry about class loader issues will make all the difference.