0

我想使用 crudRepository 从数据库中删除一个条目。我有一个 Timecard 实体,它与 Task 实体具有一对多关系,而 Task 与 Project 具有一对多关系。我想根据 taskId 和 projectId 删除数据库的一个元素。

以下是实体的定义:

@Entity
@Table(name = "timecards")
public class TimeCard{

    @Getter @Setter
    @Column(name = "timecard_id", nullable = false)
    @SequenceGenerator(name = "timecardSequence", sequenceName = "timecardIdSequence", allocationSize = 1, initialValue = 1)
    @GeneratedValue(generator = "timecardSequence")
    @Id
    private Long timeCardId;

    @Getter @Setter
    @ManyToOne
    @JoinColumn(name = "task_id", nullable = false, referencedColumnName = "id") 
    private Task task;
}
@Entity
@Table(name = "tasks")
public class Task{

    @Getter @Setter
    @Column(name = "id", nullable = false)
    @SequenceGenerator(name = "taskSequence", sequenceName = "taskIdSequence", allocationSize = 1, initialValue = 1)
    @GeneratedValue(generator = "taskSequence")
    @Id
    private Long id;

    @Getter
    @Setter
    @Column(name = "task_id", nullable = false)
    private int taskId;

    @Getter @Setter
    @ManyToOne
    @JoinColumn(name = "project_id", referencedColumnName = "project_id")
    private Project project;
}
@Entity
@Table(name = "projects")
public class Project implements Serializable {
   
    @Getter @Setter
    @Column(name = "project_id", nullable = false)
    @Id
    private Long projectId;
@Repository
public interface TimeCardRepository extends CrudRepository<TimeCard, String>{
 public void deleteTimeCardByTaskTaskIdAndTaskProjectProjectId(int taskId, Long projectId);
}

deleteTimeCardByTaskTaskIdAndTaskProjectProjectId 是我的属性表达式,但不幸的是我收到以下错误消息:

Caused by: io.quarkus.spring.data.deployment.UnableToParseMethodException: Entity com.timecardservice.timecard.TimeCard does not contain a field named: TaskProject_ProjectId. Offending method is deleteTimeCardByTaskTaskIdAndTaskProjectProjectId

我检查了https://evonsdesigns.medium.com/spring-jpa-one-to-many-query-examples-281078bc457b的横断点,但我收到了相同的错误消息。

执行此查询的正确方法是什么?

4

3 回答 3

1

如果 JPA 方式不够用,您始终可以使用 NativeQuery/JPQLQuery 来完成这项工作:

@Modifying
@Query(value = "DELETE FROM TimeCard tc WHERE tc.task.taskId = :taskId and tc.task.project.projectId = :projectId")
List<Long> deleteByIds(@Param("taskId") Long taskId, @Param("projectId") Long projectId);
于 2021-08-09T13:45:26.990 回答
1

您需要使用 _ 来访问连接表/实体的属性。

所以应该是

public void deleteTimeCardByTask_TaskIdAndTask_Project_ProjectId(int taskId, Long projectId);

您还可以使用以下命令检查记录的 sql 语句

spring.jpa.show-sql=true
于 2021-08-09T13:36:58.870 回答
0

您可以显式编写查询:

@Repository
public interface TimeCardRepository extends CrudRepository<TimeCard, String>{

  @Modifying 
  @Query(value = "DELETE FROM tc timeCard WHERE tc IN ( "
                 + "SELECT timeCard FROM timeCard  "
                 + " JOIN timeCard.task task "
                 + " JOIN taks.project project "
                 + " WHERE task.id = :taskId AND project.id= :projectId)") 
 public void deleteTimeCardByTaskTaskIdAndTaskProjectProjectId(int taskId, Long projectId);
}
于 2021-08-09T13:48:52.253 回答