gpt4 book ai didi

java - Spring Mvc-从数据库中删除用户

转载 作者:行者123 更新时间:2023-11-29 01:52:09 25 4
gpt4 key购买 nike

我正在开发小型 Spring MVC 应用程序,我在从 mysql 数据库中删除客户时遇到问题。当我像管理员一样删除客户时,客户仅在客户表中消失,并保留在权限和用户表中。问题是如何修复?

用户类

@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

公共(public)类权威机构{

@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

公共(public)类客户实现序列化{

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

公共(public)类 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

@事务性公共(public)类 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();

}

最佳答案

您必须在表之间添加外键约束并将操作设置为(on delete= cascade)。

关于java - Spring Mvc-从数据库中删除用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38687841/

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