Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); User user = new User(); user.setUsername("john_doe"); user.setEmail("[email protected]"); user.setPassword("password123"); session.persist(user); tx.commit(); session.close();
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); User user = session.get(User.class, 1); user.setUsername("jane_doe"); session.persist(user); tx.commit(); session.close();In this example, an existing User object is retrieved from the database and updated with a new username. The persist method is then called to save the changes to the database. Finally, the transaction is committed and the session is closed. Both examples use the org.hibernate package library, which is a popular Java library for working with database tables using Java objects.