OneToOne实体映射删除操作不起作用

时间:2019-05-12 08:33:52

标签: java hibernate jpa one-to-one

我有一个组成员表,该表以一对一关系绑定到用户表,当我尝试从组成员表中删除记录时,什么都没有发生。

我的Groups

public class Groups implements Serializable {

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

    @NotBlank
    @Column(unique = true)
    private String groupName;

    @OneToMany(fetch = FetchType.LAZY, orphanRemoval = false, mappedBy = "group")
    private List<GroupMembers> members;
}

Group Members

public class GroupMembers implements Serializable {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "group_id", nullable = false)
    private Groups group;

    @OneToOne
    @JoinColumn(name = "user_id", nullable = false)
    private User user;
}

Users

public class User implements Serializable {

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

    private String firstName;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "commutersInfo", cascade = CascadeType.ALL, orphanRemoval = true)
    private GroupMembers groupMembers;
}

当我尝试以下操作删除记录时

groupsMembersRepository.delete(groupsMembersRepository.findByUserEmail(email));

没有错误,控制台中没有调试信息,只是没有从数据库中删除记录

1 个答案:

答案 0 :(得分:0)

由于某种原因,JPA没有运行我的删除命令,因此当我执行spring.jpa.show-sql=true时,我会看到所有选择查询,而JPA没有运行我的删除查询,我不知道为什么,但是我想要一些解释。

最终我不得不使用

@Modifying
    @Query(value = "DELETE FROM `group_members` WHERE `commuters_info_id` = ?1", nativeQuery = true)
    public void deleteByCommutersInfoEmail(long id);