1

Hello I want to delete the entyties from db. But There is some problem.

DB diagram

DB diagram

ACCOUNT Entity

@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

USER Entity

@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

Account Service

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

and 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";
    }
}

I tried to delete object with accountRepository.delete(account) - same result accountRepository.deleteById(accountById) - same result

I added @Transactional only on delete method because Spring asked me that.

{
    "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
}

I think I have a problems in my relations between entities. Thank You.

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

UPDATE: Another operations (Save, Update, Get) works good.

2
  • Please show the repository method Commented Apr 1, 2020 at 9:11
  • Yes sure. Sorry for that. I hae already updated my question. Commented Apr 1, 2020 at 9:17

1 Answer 1

1

I found a solution. I put custom query above deleteById method

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

But anyway I didn't understand why it doesn't work without custom queries with default delete or deleteById methods.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.