0

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)
);

2 Answers2

0

You defined two entities in service.xml but the service builder will create 3 tables and the third one is mapping tables which map the Employee and Department primary keys.

That table will hold the records of your employee and department respectively. So when you getEmployeeDepartment(employeeId) it will return the list of Departments and vice versa.

UPDATE I was able to find the same error in this topic via the Liferay Dev community here https://liferay.dev/ask/questions/development/re-service-builder-persistance-impl-errors-in-case-of-tablemapper-5

The issue should be fixed after we add the column <column name="companyId" type="long" /> into both two entities.

bui vy
  • 30
  • 6
  • Thanks for your response , That's right, But the thing is in third table implicitly generate one extra column by name CompanyId, that was not genereted by me. Because of this i got error in persistanceImpl class. – Rakshith m Oct 04 '22 at 06:25
  • @Override public void addEmployees(long pk, long[] employeePKs) { long companyId = 0; Department department = fetchByPrimaryKey(pk); if (department == null) { companyId = CompanyThreadLocal.getCompanyId();//------> this line //auto created in DepartmentpersistenceImpl class } else { companyId = department.getCompanyId(); } departmentToEmployeeTableMapper.addTableMappings( companyId, pk, employeePKs); } – Rakshith m Oct 04 '22 at 06:28
  • Error shows in ---> CompanyThreadLocal.getCompanyId(); this line – Rakshith m Oct 04 '22 at 06:29
  • @Rakshithm companyId is an auto-generated parameter, it will be added/updated once a new record for Employee_department table is created/edited. – bui vy Oct 04 '22 at 07:52
  • @Rakshithm can you post here what was the error on this line? `companyId = CompanyThreadLocal.getCompanyId()` – bui vy Oct 04 '22 at 07:55
  • In EmployeePersistenceImpl.java it shows error like [The method getCompanyId() is undefined for the type Employee] . In DepartmentPersistanceImpl.java it shows like [The method getCompanyId() is undefined for the type Department] – Rakshith m Oct 10 '22 at 12:49
  • Understood. @Rakshithm, could you add `` into both two entities? – bui vy Oct 11 '22 at 04:00
  • Same issue here: https://liferay.dev/ask/questions/development/re-service-builder-persistance-impl-errors-in-case-of-tablemapper-5 – bui vy Oct 11 '22 at 04:01
  • Thanks for quick response, Now its working , above one is the solution for generate third table in liferay. once again thanks for your response. – Rakshith m Oct 14 '22 at 06:18
  • @Rakshithm if it is working for you, could you mark my answer is accepted so anyone who has the same issue could see it? – bui vy Oct 16 '22 at 01:02
0

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.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90