我正在開發小型Spring MVC應用程序,並且我在從mysql數據庫中刪除客戶時遇到了問題。當我像管理員一樣刪除客戶時,客戶只會在客戶表中消失並保留在權限和用戶表中。問題是如何修復它?Spring Mvc-從數據庫中刪除用戶
User.class
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int usersId;
private String username;
private String password;
private Boolean enabled;
private int customerId;
public int getUsersId() {
return usersId;
}
public void setUsersId(int usersId) {
this.usersId = usersId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
}
@Entity
公共類機構{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int authoritiesId;
private String username;
private String authority;
public int getAuthoritiesId() {
return authoritiesId;
}
public void setAuthoritiesId(int authoritiesId) {
this.authoritiesId = authoritiesId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
}
@Entity
公共類客戶實現Serializable {
private static final long serialVersionUID = 5140900014886997914L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int customerId;
@NotEmpty(message = "Nazwa użytkownika nie może pozostać pusta!")
private String customerName;
@NotEmpty(message = "Uzupełnij adres email!")
private String customerEmail;
private String customerPhone;
@NotEmpty(message = "Nazwa użytkownika nie może pozostać pusta!")
private String username;
@NotEmpty(message = "Uzupełnij hasło!")
@Size(min = 6, max = 16, message = "Hasło musi zawierać od 6 do 16 znaków!")
private String password;
private boolean enabled;
@OneToOne(mappedBy = "customer", cascade = CascadeType.REMOVE)
@JoinColumn(name = "billingAddressId")
private BillingAddress billingAddress;
@OneToOne(mappedBy = "customer", cascade = CascadeType.REMOVE)
@JoinColumn(name = "shippingAddressId")
private ShippingAddress shippingAddress;
@OneToOne(cascade = CascadeType.REMOVE, mappedBy = "customer")
@JoinColumn(name = "cartId")
@JsonIgnore
private Cart cart;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerEmail() {
return customerEmail;
}
public void setCustomerEmail(String customerEmail) {
this.customerEmail = customerEmail;
}
public String getCustomerPhone() {
return customerPhone;
}
public void setCustomerPhone(String customerPhone) {
this.customerPhone = customerPhone;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public BillingAddress getBillingAddress() {
return billingAddress;
}
public void setBillingAddress(BillingAddress billingAddress) {
this.billingAddress = billingAddress;
}
public ShippingAddress getShippingAddress() {
return shippingAddress;
}
public void setShippingAddress(ShippingAddress shippingAddress) {
this.shippingAddress = shippingAddress;
}
public Cart getCart() {
return cart;
}
public void setCart(Cart cart) {
this.cart = cart;
}
}
@Controller
公共類RegisterController {
@RequestMapping("/register")
public String registerCustomer(Model model) {
Customer customer = new Customer();
BillingAddress billingAddress = new BillingAddress();
ShippingAddress shippingAddress = new ShippingAddress();
customer.setBillingAddress(billingAddress);
customer.setShippingAddress(shippingAddress);
model.addAttribute("customer", customer);
return "registerCustomer";
}
@Autowired
private CustomerService customerService;
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String registerCustomerPost(@Valid @ModelAttribute("customer") Customer customer, BindingResult result,
Model model) {
if (result.hasErrors()) {
return "registerCustomer";
}
List<Customer> customerList = customerService.getAllCustomers();
for (int i = 0; i < customerList.size(); i++) {
if (customer.getCustomerEmail().equals(customerList.get(i).getCustomerEmail())) {
model.addAttribute("emailMsg", "Email już istnieje w bazie danych!");
return "registerCustomer";
}
if (customer.getUsername().equals(customerList.get(i).getUsername())) {
model.addAttribute("usernameMsg", "Użytkownik o dane nazwie już istnieje w bazie!");
return "registerCustomer";
}
}
customer.setEnabled(true);
customerService.addCustomer(customer);
return "registerCustomerSuccess";
}
}
@Repository
@Transactional 酒館LIC類CustomerDaoImpl實現CustomerDao {
@Autowired
private SessionFactory sessionFactory;
public void addCustomer(Customer customer) {
Session session = sessionFactory.getCurrentSession();
customer.getBillingAddress().setCustomer(customer);
customer.getShippingAddress().setCustomer(customer);
session.saveOrUpdate(customer);
session.saveOrUpdate(customer.getBillingAddress());
session.saveOrUpdate(customer.getShippingAddress());
Users newUser = new Users();
newUser.setUsername(customer.getUsername());
newUser.setPassword(customer.getPassword());
newUser.setEnabled(true);
newUser.setCustomerId(customer.getCustomerId());
Authorities newAuthority = new Authorities();
newAuthority.setAuthority("ROLE_USER");
session.saveOrUpdate(newUser);
session.saveOrUpdate(newAuthority);
newAuthority.setUsername(customer.getUsername());
Cart newCart = new Cart();
newCart.setCustomer(customer);
customer.setCart(newCart);
session.saveOrUpdate(customer);
session.saveOrUpdate(newCart);
session.flush();
}
public Customer getCustomerById(int id) {
Session session = sessionFactory.getCurrentSession();
Customer customer = (Customer) session.get(Customer.class, id);
session.flush();
return customer;
}
public List<Customer> getAllCustomers() {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from Customer ");
List<Customer> customerList = query.list();
return customerList;
}
public Customer getCustomerByUsername(String username) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from Customer where username = ?");
query.setString(0, username);
return (Customer) query.uniqueResult();
}
public void deleteCustomer(Customer customer) {
Session session = sessionFactory.getCurrentSession();
session.delete(customer);
session.flush();
}
}
刪除用戶的代碼將比實體更有用。 – dunni