2016-07-31 378 views
2

我正在開發小型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(); 

} 

}

+0

刪除用戶的代碼將比實體更有用。 – dunni

回答

1

你得表並設定行動(上刪除=級聯)之間添加外鍵約束。

+0

餐桌用戶和顧客之間?怎麼做? –

1

您的客戶實體與用戶和權限表沒有任何關係,那麼jpa不會執行刪除操作。 編輯 在客戶實體,而不是用戶名,你可以有

@OneToOne(mappedBy="username", cascadeType=CascadeType.REMOVE) private User user 

同爲權威作者單位:

@OneToOne(mappedBy="username", cascadeType=CascadeType.REMOVE) private User user 
+0

我應該申請什麼樣的關係?在哪些類或表之間? –

+0

我無法從客戶類中刪除用戶名,因爲它在註冊過程中是必需的 –

+0

您不會刪除用戶名,只需通過OneToOne relarionhip將其封裝在用戶對象中。 – angcap

1

在您看來,用戶和客戶之間的關係是一個one.And你想在刪除客戶時刪除用戶。

首先我們必須明確的關係。

在用戶實體中,我建議您刪除CustomerId,並將用戶用戶添加到客戶實體。

和客戶實體將是:

private User user; 
public void setUser(User user){ 
    this.user = user; 
} 
@OneToOne 
public User getUser(){ 
    return this.user; 
} 

之後,你可以得到由客戶(用戶ID)的用戶,當你要刪除的客戶,你應該得到的用戶,然後再刪除客戶並刪除該用戶

+0

而其他類的修改呢? –