(Delete) Use session's delete() method to delete information from the database

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();
	}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326066000&siteId=291194637