Search in sources :

Example 1 with PersistenceUtil

use of javax.persistence.PersistenceUtil in project hibernate-orm by hibernate.

the class PersistenceContextTest method test.

@Test
public void test() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        SessionImplementor sessionImplementor = entityManager.unwrap(SessionImplementor.class);
        SessionFactory sessionFactory = entityManager.getEntityManagerFactory().unwrap(SessionFactory.class);
    });
    Long _personId = doInJPA(this::entityManagerFactory, entityManager -> {
        entityManager.createQuery("delete from Book").executeUpdate();
        entityManager.createQuery("delete from Person").executeUpdate();
        Person person = new Person();
        person.setId(1L);
        person.setName("John Doe");
        entityManager.persist(person);
        entityManager.remove(person);
        entityManager.persist(person);
        Long personId = person.getId();
        Book book = new Book();
        book.setAuthor(entityManager.getReference(Person.class, personId));
        return personId;
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Long personId = _personId;
        Person person = entityManager.find(Person.class, personId);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        entityManager.createQuery("delete from Book").executeUpdate();
        entityManager.createQuery("delete from Person").executeUpdate();
        Person person = new Person();
        person.setId(1L);
        person.setName("John Doe");
        session.save(person);
        session.delete(person);
        session.save(person);
        Long personId = person.getId();
        Book book = new Book();
        book.setId(1L);
        book.setIsbn("123-456-7890");
        entityManager.persist(book);
        book.setAuthor(session.load(Person.class, personId));
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        Long personId = _personId;
        Person person = session.get(Person.class, personId);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        Long personId = _personId;
        Person person = session.byId(Person.class).load(personId);
        Optional<Person> optionalPerson = session.byId(Person.class).loadOptional(personId);
        String isbn = "123-456-7890";
        Book book = session.bySimpleNaturalId(Book.class).getReference(isbn);
        assertNotNull(book);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        String isbn = "123-456-7890";
        Book book = session.byNaturalId(Book.class).using("isbn", isbn).load();
        assertNotNull(book);
        Optional<Book> optionalBook = session.byNaturalId(Book.class).using("isbn", isbn).loadOptional();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Long personId = _personId;
        Person person = entityManager.find(Person.class, personId);
        person.setName("John Doe");
        entityManager.flush();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Long personId = _personId;
        Person person = entityManager.find(Person.class, personId);
        entityManager.createQuery("update Person set name = UPPER(name)").executeUpdate();
        entityManager.refresh(person);
        assertEquals("JOHN DOE", person.getName());
    });
    try {
        doInJPA(this::entityManagerFactory, entityManager -> {
            Long personId = _personId;
            try {
                Person person = entityManager.find(Person.class, personId);
                Book book = new Book();
                book.setId(100L);
                book.setTitle("Hibernate User Guide");
                book.setAuthor(person);
                person.getBooks().add(book);
                entityManager.refresh(person);
            } catch (EntityNotFoundException expected) {
                log.info("Beware when cascading the refresh associations to transient entities!");
            }
        });
    } catch (Exception expected) {
    }
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        Long personId = _personId;
        Person person = session.byId(Person.class).load(personId);
        person.setName("John Doe");
        entityManager.flush();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        Long personId = _personId;
        Person person = session.byId(Person.class).load(personId);
        session.doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("UPDATE Person SET name = UPPER(name)");
            }
        });
        session.refresh(person);
        assertEquals("JOHN DOE", person.getName());
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        Long personId = _personId;
        Person person = session.byId(Person.class).load(personId);
        session.clear();
        person.setName("Mr. John Doe");
        session.lock(person, LockMode.NONE);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        Long personId = _personId;
        Person person = session.byId(Person.class).load(personId);
        session.clear();
        person.setName("Mr. John Doe");
        session.saveOrUpdate(person);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        Long personId = _personId;
        Person personDetachedReference = session.byId(Person.class).load(personId);
        session.clear();
        new MergeVisualizer(session).merge(personDetachedReference);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Long personId = _personId;
        Person person = entityManager.find(Person.class, personId);
        entityManager.clear();
        person.setName("Mr. John Doe");
        person = entityManager.merge(person);
        boolean contained = entityManager.contains(person);
        assertTrue(contained);
        PersistenceUnitUtil persistenceUnitUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();
        boolean personInitialized = persistenceUnitUtil.isLoaded(person);
        boolean personBooksInitialized = persistenceUnitUtil.isLoaded(person.getBooks());
        boolean personNameInitialized = persistenceUnitUtil.isLoaded(person, "name");
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Long personId = _personId;
        Person person = entityManager.find(Person.class, personId);
        PersistenceUtil persistenceUnitUtil = Persistence.getPersistenceUtil();
        boolean personInitialized = persistenceUnitUtil.isLoaded(person);
        boolean personBooksInitialized = persistenceUnitUtil.isLoaded(person.getBooks());
        boolean personNameInitialized = persistenceUnitUtil.isLoaded(person, "name");
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        Long personId = _personId;
        Person person = session.byId(Person.class).load(personId);
        session.clear();
        person.setName("Mr. John Doe");
        person = (Person) session.merge(person);
        boolean contained = session.contains(person);
        assertTrue(contained);
        boolean personInitialized = Hibernate.isInitialized(person);
        boolean personBooksInitialized = Hibernate.isInitialized(person.getBooks());
        boolean personNameInitialized = Hibernate.isPropertyInitialized(person, "name");
    });
}
Also used : SessionFactory(org.hibernate.SessionFactory) Statement(java.sql.Statement) PersistenceUnitUtil(javax.persistence.PersistenceUnitUtil) EntityNotFoundException(javax.persistence.EntityNotFoundException) EntityNotFoundException(javax.persistence.EntityNotFoundException) PersistenceUtil(javax.persistence.PersistenceUtil) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Statement (java.sql.Statement)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 PersistenceUnitUtil (javax.persistence.PersistenceUnitUtil)1 PersistenceUtil (javax.persistence.PersistenceUtil)1 Session (org.hibernate.Session)1 SessionFactory (org.hibernate.SessionFactory)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1 Test (org.junit.Test)1