Spring Boot JPA删除将返回200ok,但不会从数据库中删除

问题描述 投票:0回答:1

你好,我想从数据库中删除实体。但是有一些问题。

数据库图

DB diagram

帐户实体

@Entity
@Table(name = "compte", schema = "vshop_schema")
public class Account {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "email", nullable = false)
    @Email
    @NotEmpty()
    private String email;

    @Column(name = "pass", nullable = false)
    @NotEmpty
    @Size(min = 6)
    @PasswordFormat
    private String password;

    @Transient
    private String passwordConfirm;

    @Column(name = "active")
    private boolean active = true;

    @OneToOne(fetch = FetchType.EAGER, optional = false, cascade = CascadeType.ALL)
    @JoinColumn(name = "utilisateur_id")
    private User user;

    @ManyToOne(fetch=FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = "role_id")
    @JsonBackReference
    private Role role;

    public Account() {
    }

    getters/setters

用户实体

@Entity
@Table(name = "utilisateur", schema = "vshop_schema")
@JsonIgnoreProperties("account")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "user_name")
    @NotNull
    @Pattern(regexp = "^[a-zA-Z0-9._-]{3,}$", message = "Ce n'est pas un pseudo")
    private String userName;


    @Column(name = "date_inscription")
    @CreationTimestamp
    @JsonFormat(pattern="dd/MM/yyyy")
    private Date dateRegistration;

    @OneToOne(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Account account;

    @OneToOne(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private UserInfo userInfo;

    public User() {
    }

    getters/setters

帐户服务

@Override
@Transactional
public void deleteAccount(int userId) {
    accountRepository.deleteAccountByUserId(userId);
}

和RestController

@RestController
@RequestMapping("/api")
public class AccountRestController {

    @Autowired
    private AccountRepository repository;

    ...
    @DeleteMapping("/users/{userId}/accounts")
    public String deleteAccount(@PathVariable int userId) {
            accountService.deleteAccount(userId);
            return "Deleted Succefully";
    }
}

我试图用accountRepository.delete(account)-相同的结果accountRepository.deleteById(accountById)-相同结果

我仅在删除方法上添加了@Transactional,因为Spring要求我这样做。

{
    "status": 500,
    "message": "No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call",
    "timestamp": 1585730989245
}

我认为实体之间的关系存在问题。谢谢。

更新:AccountRepository

public interface AccountRepository extends JpaRepository<Account, Integer> {
    Account findByEmail(String email);

    Account findByUser(User user);

    @Query(value = "select email from vshop_schema.compte u where u.utilisateur_id = ?1", nativeQuery = true)
    String findEmailByUserId(int id);

    void deleteAccountByUserId(int id);
}

更新:另一个操作(保存,更新,获取)效果很好。

java spring-boot spring-data-jpa one-to-one http-delete
1个回答
0
投票

我找到了解决方案。我将自定义查询放在deleteById方法上方

@Modyfing
@Query("delete from /name-of-table/ n where n./column-name/ = ?1", nativeQuery=true)
void deleteById (int id);

但是无论如何,我不明白为什么不使用默认的delete或deleteById方法进行自定义查询就无法正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.