🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this article, we will discuss various ways to update JPA entity objects into a database.
Modifying existing entity objects that are stored in the database is based on transparent persistence, which means that changes are detected and handled automatically.
Learn complete JPA at JPA Tutorial - Java Persistence API
Learn Hibernate ORM Framework at Hibernate Tutorial
This article covers the following topics:
- Transparent Update
- UPDATE Queries
1. Transparent Update
Once an entity object is retrieved from the database (no matter which way) it can simply be modified in memory from inside an active transaction. In this example, we use a Student JPA entity to perform an update database operation.
public void updateEntity() {
EntityManager entityManager = JPAUtil.getEntityManagerFactory().createEntityManager();
entityManager.getTransaction().begin();
Student student = entityManager.find(Student.class, 1);
System.out.println("student id :: " + student.getId());
System.out.println("student firstname :: " + student.getFirstName());
System.out.println("student lastname :: " + student.getLastName());
System.out.println("student email :: " + student.getEmail());
// The entity object is physically updated in the database when the transaction
// is committed
student.setFirstName("Ram");
student.setLastName("jadhav");
entityManager.getTransaction().commit();
entityManager.close();
}
Refer JPA CRUD Example article for a complete example.
The entity object is physically updated in the database when the transaction is committed. If the transaction is rolled back and not committed the update is discarded.
2. UPDATE Queries
UPDATE queries provide an alternative way for updating entity objects in the database. Modifying objects using an UPDATE query may be useful especially when many entity objects have to be modified in one operation.
public void updateEntity() {
EntityManager entityManager = JPAUtil.getEntityManagerFactory().createEntityManager();
entityManager.getTransaction().begin();
Query query = entityManager
.createQuery("UPDATE Student e SET e.firstName = 'Ram' WHERE e.id = :id");
query.setParameter("id", 1);
int rowsUpdated = query.executeUpdate();
System.out.println("entities Updated: " + rowsUpdated);
entityManager.getTransaction().commit();
entityManager.close();
}
On success - the executeUpdate method returns the number of objects that have been modified by the query.
Refer complete JPA CRUD operations example at JPA CRUD Example
Learn complete JPA at JPA Tutorial - Java Persistence API
Learn Hibernate ORM Framework at Hibernate Tutorial
Related Articles
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment