public void testDelete() { //1. Call the tool class (the factory class of the session) and get the sessionfactory SessionFactory sessionF = HiberTool.getSessionFactory(); //2. Get session Session session = sessionF.openSession(); //3. Open the transaction Transaction tx = session.beginTransaction(); //4. According to the id query, get the User object (emphasis) User user = session.get(User.class, 2);//Get the object first session.delete(user);//Remove the object //5. Commit the transaction tx.commit(); //6. Close session.close(); sessionF.close(); }
There is also a delete method:
public void testDelete() { //1. Call the tool class (the factory class of the session) and get the sessionfactory SessionFactory sessionF = HiberTool.getSessionFactory(); //2. Get session Session session = sessionF.openSession(); //3. Open the transaction Transaction tx = session.beginTransaction(); User user = new User();//Instantiate the object first user.setUid(3);//Determine the primary key value of the object session.delete(user);//Remove the object //5. Commit the transaction tx.commit(); //6. Close session.close(); sessionF.close(); }