rawdog
rawdog

Reputation: 732

Liferay Service Builder many to many relationship with custom column

I have a question relating to the Liferay Service Builder. I want to create a many-to-many relationship with custom colums in the created Join table.

This is my service.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
<service-builder package-path="de.mycompany.mm.services">
    <author>rawdog</author>
    <namespace>mycompany</namespace>

    <entity name="Registration" local-service="true" remote-service="false">
        <column name="RegID" type="long" primary="true"></column>
        <column name="Email" type="String"></column>
        <column name="Hash" type="String"></column>
        <column name="Validate" type="boolean"></column>
        <column name="NewsletterID" type="Collection" entity="Newsletter" mapping-table="Registration_Newsletter"/>
        <finder name="Hash" return-type="Registration">
            <finder-column name="Hash"></finder-column>
        </finder>
    </entity>

    <entity name="Newsletter" local-service="true" remote-service="false">
        <column name="NewsletterID" type="long" primary="true"></column>
        <column name="Name" type="String"></column>
        <column name="Status" type="boolean"></column>
        <column name="RegID" type="Collection" entity="Registration" mapping-table="Registration_Newsletter"/>
    </entity>
</service-builder>

This automatically creates following SQL CREATE statement:

create table mycompany_Registration_Newsletter (
    RegID LONG not null,
    NewsletterID LONG not null,
    primary key (ANID, MMID)
);

How could I also automatically create custom colums in the JOIN table like Status BOOLEAN. It should look like this in the end:

create table mycompany_Registration_Newsletter (
    RegID LONG not null,
    NewsletterID LONG not null,
    Status BOOLEAN,
    primary key (ANID, MMID)
);

Thanks for any help.

Upvotes: 2

Views: 785

Answers (1)

BigWave IT
BigWave IT

Reputation: 11

You have two ways to solve this problem :

  1. You can manually modify your joined table by adding your boolean column (execute sql script on your database : ALTER TABLE yourJoinedTable ADD yourBooleanColumn BOOLEAN NOT NULL;) and then define CUSTOM SQL / NATIVE QUERY IN SERVICE BUILDER to use this joined table.
  2. (If you want Service Builder to handle this joined table as others tables) to define it as an entity in your service.xml so that you'll be able to use it easily once you had defined 2 OneToMany relationship "right side" and "left side" of your joined table (assuming that a ManyToMany = is "equal" to = oneToMany left side && ManyToOne right side).

Upvotes: 1

Related Questions