gpt4 book ai didi

java - Spring Boot JPA删除返回200 ok但不能从数据库中删除

转载 作者:行者123 更新时间:2023-12-01 17:40:10 27 4
gpt4 key购买 nike

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

数据库图

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
}

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

更新:帐户存储库

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);
}

更新:其他操作(保存、更新、获取)效果很好。

最佳答案

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

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

但无论如何,我不明白为什么如果没有使用默认删除或deleteById方法的自定义查询,它就不起作用。

关于java - Spring Boot JPA删除返回200 ok但不能从数据库中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60967142/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com