Search code examples
liferayliferay-7liferay-dxpliferay-7.1

How to create a schema and how to build relationship between tables in liferay dxp?


I have done many-many relation between Employee and Department table by using mapped-table attribute ,It was generated third table by name Employee_department, In Employee_department table why liferay generate one extra column by name CompanyId and it leads error in persistenceImpl class.

<service-builder package-path="com_m2" auto-namespace-tables="false">
    <namespace>emp</namespace>


   <entity name="Employee" local-service="true" remote-service="false" table="Employee" uuid="true">
        <column name="eid" type="long" primary="true"></column>
        <column name="name" type="String"></column>
        <column name="address" type="String"></column>
        <column name="deptid" type="Collection" entity="Department" mapping-table="Employee_department"/>
   </entity>
   
    <entity name="Department" local-service="true" remote-service="false" table="Department" uuid="true">
        <column name="deptid" type="long" primary="true"></column>
        <column name="department" type="String"></column>
        <column name="eid" type="Collection" entity="Employee" mapping-table="Employee_department"/>
    </entity>
    
</service-builder>

-------------------------------------------------------------------------------

create table Employee_department (
    CompanyId LONG not null,
    deptid LONG not null,
    eid LONG not null,
    primary key (deptid, eid)
);

Solution

  • A "company" in the Liferay API can be found as "Virtual Instance" on the UI: You can have multiple completely separate collections of data, for multi-tenancy.

    I'm not sure why the field is generated when your two entities don't have such a field - that might be a bug. But it points to what you could do to blend your entities into Liferay's stock entities. This will make it easier to also use them from Liferay's Asset- and Info-Frameworks.

    A whole new (and different) solution would be to leverage Liferay Objects and go with client-side code - but that's just here for completeness and not what you're asking for.