by vips7L · 5y ago · 0 comments
https://docs.jboss.org/hibernate/stable/orm/userguide/html_s...
> using setter of an instance shouldn't update in database by default omg
This doesn't happen.. the database won't update until you ask the entity manager to persist the entity.
> I've seen one project where transaction leaked to Jackson!! Jackson was calling getters on fields and executing DB queries. JSON ended up as 2.6Mb instead list of 10 fields.
I suspect you might not agree, but JPA lazy loading is one of the easiest concepts to understand. However, I'd argue that leaking your database models to the view is a mistake to begin with and said application is already incorrect.
> If in any doubt, refer to JOOQ - it's the SQL-oriented ORM for Java.
Both the author/maintainer of jOOq and Hibernate agree that each has their place.
I really am amazed at developers that don't take the time to learn about the tools they use.
Doesn't this happen through automatic dirty checking?
Obviously JPA knows what fields were updated via dirty checking.. that's almost half the point of an ORM.
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Person person = session.load(Person.class, 2); //loads Person object for id 2
person.setAge(32);
tx.commit();
session.close();
HibernateUtil.closeSessionFactory();
It results in an updated age for the person in the DB. However, EntityManager was never directly notified about the change in the person object.[1] - https://learnjava.co.in/automatic-dirty-checking-in-hibernat...
That is not how Hibernate or JPA is used normally. It is going out of standard way to achieve the thing you complain about.