【问题标题】:How to create a schema and how to build relationship between tables in liferay dxp?如何创建模式以及如何在 liferay dxp 中的表之间建立关系?
【发布时间】:2022-10-04 20:48:52
【问题描述】:

我已经使用映射表属性在 Employee 和 Department 表之间建立了多对多关系,它是按名称 Employee_department 生成的第三个表,在 Employee_department 表中,为什么 liferay 会按名称 CompanyId 生成一个额外的列,它会导致 persistenceImpl 类中的错误。

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

【问题讨论】:

    标签: liferay liferay-7 liferay-dxp liferay-7.1


    【解决方案1】:

    您在 service.xml 中定义了两个实体,但服务构建器将创建 3 个表,第三个是映射表,它映射 Employee 和 Department 主键。

    该表将分别保存您的员工和部门的记录。因此,当您 getEmployeeDepartment(employeeId) 时,它将返回部门列表,反之亦然。

    【讨论】:

    • 感谢您的回复,没错,但事情是在第三个表中隐式生成一个名为 CompanyId 的额外列,这不是我生成的。因此,我在persistanceImpl 类中遇到错误。
    • @Override public void addEmployees(long pk, long[] employeePKs) { long companyId = 0;部门部门 = fetchByPrimaryKey(pk); if (department == null) { companyId = CompanyThreadLocal.getCompanyId();//-----> 这一行 //auto created in DepartmentpersistenceImpl class } else { companyId = department.getCompanyId(); } departmentToEmployeeTableMapper.addTableMappings(companyId, pk, employeePKs); }
    • 错误显示在 ---> CompanyThreadLocal.getCompanyId();这条线
    • @Rakshithm companyId 是一个自动生成的参数,一旦创建/编辑了 Employee_department 表的新记录,它将被添加/更新。
    • @Rakshithm 你能在这里发布这条线的错误是什么吗? companyId = CompanyThreadLocal.getCompanyId()
    【解决方案2】:

    Liferay API 中的“公司”可以在 UI 上作为“虚拟实例”找到:您可以拥有多个完全独立的数据集合,用于多租户。

    我不确定为什么当您的两个实体没有这样的字段时会生成该字段 - 这可能是一个错误。但它指出了你可以做些什么来将你的实体融入 Liferay 的股票实体。这将更容易从 Liferay 的资产和信息框架中使用它们。

    一个全新的(和不同的)解决方案是利用 Liferay 对象并使用客户端代码——但这只是为了完整性,而不是你所要求的。

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 2019-03-25
      • 2017-07-09
      • 2019-06-06
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      相关资源
      最近更新 更多