Search in sources :

Example 1 with PersistenceUnitUtil

use of javax.persistence.PersistenceUnitUtil in project javaee7-samples by javaee-samples.

the class EntityGraphTest method testEntityGraphWithNamedParameters.

@Test
public void testEntityGraphWithNamedParameters() throws Exception {
    PersistenceUnitUtil persistenceUnitUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();
    List<Movie> listMovieById = movieBean.listMoviesById(1, "javax.persistence.fetchgraph", "movieWithActors");
    assertFalse(listMovieById.isEmpty());
    assertEquals(1, listMovieById.size());
    for (Movie movie : listMovieById) {
        assertTrue(movie.getId().equals(1));
        assertTrue(persistenceUnitUtil.isLoaded(movie, "movieActors"));
        assertFalse(movie.getMovieActors().isEmpty());
        for (MovieActor movieActor : movie.getMovieActors()) {
            assertFalse(persistenceUnitUtil.isLoaded(movieActor, "movieActorAwards"));
        }
        // https://hibernate.atlassian.net/browse/HHH-8776
        // The specification states that by using fetchgraph, attributes should stay unloaded even if defined as
        // EAGER (movieDirectors), but specification also states that the persistence provider is allowed to fetch
        // additional state.
        assertTrue(persistenceUnitUtil.isLoaded(movie, "movieDirectors") || !persistenceUnitUtil.isLoaded(movie, "movieDirectors"));
        assertFalse(persistenceUnitUtil.isLoaded(movie, "movieAwards"));
    }
}
Also used : PersistenceUnitUtil(javax.persistence.PersistenceUnitUtil) Test(org.junit.Test)

Example 2 with PersistenceUnitUtil

use of javax.persistence.PersistenceUnitUtil in project javaee7-samples by javaee-samples.

the class EntityGraphTest method testEntityGraphWithNamedParametersList.

@Test
public void testEntityGraphWithNamedParametersList() throws Exception {
    PersistenceUnitUtil persistenceUnitUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();
    // Hibernate fails mixing Entity Graphs and Named Parameters with "IN". Throws NPE
    List<Movie> listMoviesByIds = movieBean.listMoviesByIds(Arrays.asList(1, 2), "javax.persistence.fetchgraph", "movieWithActors");
    assertFalse(listMoviesByIds.isEmpty());
    assertEquals(2, listMoviesByIds.size());
    for (Movie movie : listMoviesByIds) {
        assertTrue(movie.getId().equals(1) || movie.getId().equals(2));
        assertTrue(persistenceUnitUtil.isLoaded(movie, "movieActors"));
        assertFalse(movie.getMovieActors().isEmpty());
        for (MovieActor movieActor : movie.getMovieActors()) {
            assertFalse(persistenceUnitUtil.isLoaded(movieActor, "movieActorAwards"));
        }
        // https://hibernate.atlassian.net/browse/HHH-8776
        // The specification states that by using fetchgraph, attributes should stay unloaded even if defined as
        // EAGER (movieDirectors), but specification also states that the persistence provider is allowed to fetch
        // additional state.
        assertTrue(persistenceUnitUtil.isLoaded(movie, "movieDirectors") || !persistenceUnitUtil.isLoaded(movie, "movieDirectors"));
        assertFalse(persistenceUnitUtil.isLoaded(movie, "movieAwards"));
    }
}
Also used : PersistenceUnitUtil(javax.persistence.PersistenceUnitUtil) Test(org.junit.Test)

Example 3 with PersistenceUnitUtil

use of javax.persistence.PersistenceUnitUtil in project javaee7-samples by javaee-samples.

the class EntityGraphTest method testEntityGraphMovieWithActorsAndAwards.

@Test
public void testEntityGraphMovieWithActorsAndAwards() throws Exception {
    PersistenceUnitUtil persistenceUnitUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();
    List<Movie> listMoviesWithActorsFetch = movieBean.listMovies("javax.persistence.fetchgraph", "movieWithActorsAndAwards");
    for (Movie movie : listMoviesWithActorsFetch) {
        assertTrue(persistenceUnitUtil.isLoaded(movie, "movieActors"));
        assertFalse(movie.getMovieActors().isEmpty());
        for (MovieActor movieActor : movie.getMovieActors()) {
            assertTrue(persistenceUnitUtil.isLoaded(movieActor, "movieActorAwards") || !persistenceUnitUtil.isLoaded(movieActor, "movieActorAwards"));
        }
        // https://hibernate.atlassian.net/browse/HHH-8776
        // The specification states that by using fetchgraph, attributes should stay unloaded even if defined as
        // EAGER (movieDirectors), but specification also states that the persistence provider is allowed to fetch
        // additional state.
        assertTrue(persistenceUnitUtil.isLoaded(movie, "movieDirectors") || !persistenceUnitUtil.isLoaded(movie, "movieDirectors"));
        assertFalse(persistenceUnitUtil.isLoaded(movie, "movieAwards"));
    }
    List<Movie> listMoviesWithActorsLoad = movieBean.listMovies("javax.persistence.loadgraph", "movieWithActorsAndAwards");
    for (Movie movie : listMoviesWithActorsLoad) {
        // https://java.net/jira/browse/GLASSFISH-21200
        // Glassfish is not processing "javax.persistence.loadgraph".
        assertTrue(persistenceUnitUtil.isLoaded(movie, "movieActors"));
        assertFalse(movie.getMovieActors().isEmpty());
        for (MovieActor movieActor : movie.getMovieActors()) {
            assertTrue(persistenceUnitUtil.isLoaded(movieActor, "movieActorAwards"));
        }
        assertTrue(persistenceUnitUtil.isLoaded(movie, "movieDirectors"));
        assertFalse(persistenceUnitUtil.isLoaded(movie, "movieAwards"));
    }
}
Also used : PersistenceUnitUtil(javax.persistence.PersistenceUnitUtil) Test(org.junit.Test)

Example 4 with PersistenceUnitUtil

use of javax.persistence.PersistenceUnitUtil 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)

Example 5 with PersistenceUnitUtil

use of javax.persistence.PersistenceUnitUtil in project deltaspike by apache.

the class HouseRepositoryTest method shouldNotLoadLazyAssociationsWithoutGraph.

@Test
@InSequence(2)
public void shouldNotLoadLazyAssociationsWithoutGraph() throws Exception {
    House house = repository.findOptionalByName("Bellevue");
    assertNotNull(house);
    PersistenceUnitUtil puu = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();
    assertFalse(puu.isLoaded(house, "flats"));
    assertFalse(puu.isLoaded(house, "garages"));
}
Also used : PersistenceUnitUtil(javax.persistence.PersistenceUnitUtil) House(org.apache.deltaspike.data.test.ee7.domain.House) Test(org.junit.Test) InSequence(org.jboss.arquillian.junit.InSequence)

Aggregations

PersistenceUnitUtil (javax.persistence.PersistenceUnitUtil)8 Test (org.junit.Test)8 Statement (java.sql.Statement)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 PersistenceUtil (javax.persistence.PersistenceUtil)1 House (org.apache.deltaspike.data.test.ee7.domain.House)1 Session (org.hibernate.Session)1 SessionFactory (org.hibernate.SessionFactory)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1 InSequence (org.jboss.arquillian.junit.InSequence)1