@Test public void testSequenceIdGenerationInJTA() throws Exception { getTransactionManager().begin(); final EntityManager em = getFactory().createEntityManager(); Song firstSong = new Song(); firstSong.setSinger("Charlotte Church"); firstSong.setTitle("Ave Maria"); em.persist(firstSong); Song secondSong = new Song(); secondSong.setSinger("Charlotte Church"); secondSong.setTitle("Flower Duet"); em.persist(secondSong); Actor firstActor = new Actor(); firstActor.setName("Russell Crowe"); firstActor.setBestMovieTitle("Gladiator"); em.persist(firstActor); Actor secondActor = new Actor(); secondActor.setName("Johnny Depp"); secondActor.setBestMovieTitle("Pirates of the Caribbean"); em.persist(secondActor); getTransactionManager().commit(); em.clear(); getTransactionManager().begin(); firstSong = em.find(Song.class, firstSong.getId()); assertThat(firstSong).isNotNull(); assertThat(firstSong.getId()).isEqualTo(Song.INITIAL_VALUE); assertThat(firstSong.getTitle()).isEqualTo("Ave Maria"); em.remove(firstSong); secondSong = em.find(Song.class, secondSong.getId()); assertThat(secondSong).isNotNull(); assertThat(secondSong.getId()).isEqualTo(Song.INITIAL_VALUE + 1); assertThat(secondSong.getTitle()).isEqualTo("Flower Duet"); em.remove(secondSong); firstActor = em.find(Actor.class, firstActor.getId()); assertThat(firstActor).isNotNull(); assertThat(firstActor.getId()).isEqualTo(Actor.INITIAL_VALUE); assertThat(firstActor.getName()).isEqualTo("Russell Crowe"); em.remove(firstActor); secondActor = em.find(Actor.class, secondActor.getId()); assertThat(secondActor).isNotNull(); assertThat(secondActor.getId()).isEqualTo(Actor.INITIAL_VALUE + 1); assertThat(secondActor.getName()).isEqualTo("Johnny Depp"); em.remove(secondActor); getTransactionManager().commit(); em.close(); }