Spring 数据 JDBC - 多对一关系

Spring Data JDBC - Many-to-One Relationship

我似乎无法在网上找到任何关于在 Spring JDBC 中使用多对一映射的参考资料。我刚刚在文档中看到不受支持,但我不确定是否属于这种情况。

我的示例是我想将我的 AppUser 映射到特定部门。

作为参考,AppUser 使用 DEPARTMENT_ID

加入部门 table
@Table(value="m_appuser")
public class AppUserProjectionTwo {
    @Id
    private Long id;
    private String firstname;
    private String middlename;
    private String lastname;



    @Column("DEPARTMENT_ID")
    private DepartmentProjection departmenProjection;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

但是,它似乎无法正确映射。

@Table("M_DEPARTMENT")
public class DepartmentProjection {
    @Id
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

创建的查询如下所示。我一直在寻找更多相反的东西,其中 M_APPUSER.department_ID = Department.id

[SELECT "m_appuser"."ID" AS "ID", "m_appuser"."LASTNAME" AS "LASTNAME", "m_appuser"."FIRSTNAME" AS "FIRSTNAME", "m_appuser"."MIDDLENAME" AS "MIDDLENAME", "departmenProjection"."ID" AS "DEPARTMENPROJECTION_ID" FROM "m_appuser" LEFT OUTER JOIN "M_DEPARTMENT" AS "departmenProjection" ON "departmenProjection"."DEPARTMENT_ID" = "m_appuser"."ID" WHERE "m_appuser"."FIRSTNAME" = ?];

谢谢

I just saw in the documentation that is not supported but I'm not sure if this is the case.

我可以确认它不受支持。 多对一关系跨越聚合的边界。 跨聚合的引用必须建模为引用聚合的 ID。

如果您不这样做 Spring 数据 JDBC 将认为引用是一对一的关系,并且是同一聚合的一部分,这将产生您不想要的效果多对一关系,例如在删除引用实体时删除引用实体。这对于同一聚合中的一对一关系是正确的。

这在 https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates

中有更详细的解释