我正在开发小型Spring MVC应用程序,我从mysql数据库中删除客户时遇到问题。当我像管理员一样删除客户时,客户只会在客户表中消失并保留在权限和用户表中。问题是如何修复它?
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
公共类Customer实现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
public class 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 public class 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();
}
}
答案 0 :(得分:1)
您必须在表之间添加外键约束并将操作设置为(在delete = cascade上)。
答案 1 :(得分:1)
您的客户实体与用户和权限表没有任何关系,因此jpa不执行任何删除操作。 的修改 在客户实体而不是用户名中,您可以
@OneToOne(mappedBy="username", cascadeType=CascadeType.REMOVE) private User user
Authories实体也是如此:
@OneToOne(mappedBy="username", cascadeType=CascadeType.REMOVE) private User user
答案 2 :(得分:1)
根据您的意见,用户与客户之间的关系是一对一的。当您删除客户时,您想要删除用户。
首先,我们必须明确这种关系。
在用户实体中,我建议您删除CustomerId,并将用户用户添加到客户实体。
客户实体将是:
private User user;
public void setUser(User user){
this.user = user;
}
@OneToOne
public User getUser(){
return this.user;
}
之后,您可以通过客户(userId)获取用户,并且当您想要删除客户时,您应首先获得用户,然后删除客户并删除该用户