jpa 当我尝试执行delete @Query时发生异常

9vw9lbht  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(185)

我正在尝试在我的BaseRepository类中执行此@Query

@Modifying
    @Query(value = "DELETE FROM Employee e WHERE e.department.companyBranch.id = :companyBranchId")
    void deleteAllByCompanyBranchId(@Param("companyBranchId") Long companyBranchId);

字符串
但我得到了这个例外

Column "D1_0.COMPANY_BRANCH_ID" not found; SQL statement:
delete from employee where d1_0.company_branch_id=? [42122-214]] [delete from employee where d1_0.company_branch_id=?]; SQL [delete from employee where d1_0.company_branch_id=?]] with root cause

org.h2.jdbc.JdbcSQLSyntaxErrorException: Столбец "D1_0.COMPANY_BRANCH_ID" не найден
Column "D1_0.COMPANY_BRANCH_ID" not found; SQL statement:
delete from employee where d1_0.company_branch_id=? [42122-214]


我认为它与JPQL约定有某种联系,但我不确定。通过查看我的异常,我可以说查询试图在EMPLOYEE表中查找COMPANY_BRANCH_ID字段,但我不确定。
这是我的实体类:
员工:

@Entity
@Table(name = "EMPLOYEE")
@Getter
@Setter
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "DEPARTMENT_ID", nullable = false)
    private Department department;
}


@Entity
@Table(name = "DEPARTMENT")
@Getter
@Setter
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_BRANCH_ID", nullable = false)
    private CompanyBranch companyBranch;
}


公司简介

@Entity
@Table(name = "COMPANY_BRANCH")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class CompanyBranch {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
}

fafcakar

fafcakar1#

我不确定您是否可以使用这种级别的嵌套查询。
我会试着用这样的词

@Query(value = "DELETE FROM Employee e WHERE e.department.id IN (SELECT d.id from Departement d where d.companyBranch.id = :companyBranchId"))

字符串
这就是你通常在普通sql中所做的那种查询。

相关问题