Package javax.persistence

Examples of javax.persistence.EntityManagerFactory


      TypeOverridingInDialectTest.class
    );

  @Test
  public void testOverriddenTypeInDialect() throws Exception {
    final EntityManagerFactory emf = Persistence.createEntityManagerFactory( "jpajtastandalone" );

    TransactionManager transactionManager = extractJBossTransactionManager( emf );
    transactionManager.begin();
    final EntityManager em = emf.createEntityManager();
    try {
      Poem poem = new Poem();
      poem.setName( "L'albatros" );
      poem.setPoemSocietyId( UUID.randomUUID() );
      poem.setCreation( new Date() );
      em.persist( poem );
      transactionManager.commit();
      assertThat( true ).as( "Custom type not used" ).isFalse();
    }
    catch ( RollbackException e ) {
      //make this chaing more robust
      assertThat( e.getCause().getCause().getMessage() ).isEqualTo( "Exploding type" );
    }
    finally {
      try {
        transactionManager.rollback();
      }
      catch ( Exception e ) {
        //we try
      }
      em.close();
      dropSchemaAndDatabase( emf );
      emf.close();
    }
  }
View Full Code Here


  @Rule
  public PackagingRule packaging = new PackagingRule( "persistencexml/jpajtastandalone.xml", Contact.class );

  @Test
  public void testWrappedFromEntityManagerAPI() throws Exception {
    final EntityManagerFactory emf = Persistence.createEntityManagerFactory( "jpajtastandalone", TestHelper.getEnvironmentProperties() );
    assertThat( HibernateEntityManagerFactory.class.isAssignableFrom( emf.getClass() ) ).isTrue();
    SessionFactory factory = ( (HibernateEntityManagerFactory) emf ).getSessionFactory();
    assertThat( factory.getClass() ).isEqualTo( OgmSessionFactoryImpl.class );

    Session s = factory.openSession();
    assertThat( s.getClass() ).isEqualTo( OgmSessionImpl.class );
    assertThat( s.getSessionFactory().getClass() ).isEqualTo( OgmSessionFactoryImpl.class );
    s.close();

    EntityManager em = emf.createEntityManager();
    assertThat( em.unwrap( Session.class ) instanceof OgmSession );
    assertThat( em.getDelegate().getClass() ).isEqualTo( OgmSessionImpl.class );

    em.close();

    emf.close();
  }
View Full Code Here

    emf.close();
  }

  @Test
  public void testJNDIReference() throws Exception {
    final EntityManagerFactory emf = Persistence.createEntityManagerFactory( "jpajtastandalone", TestHelper.getEnvironmentProperties() );
    SessionFactory factory = ( (HibernateEntityManagerFactory) emf ).getSessionFactory();
    Reference reference = factory.getReference();
    assertThat( reference.getClassName() ).isEqualTo( OgmSessionFactoryImpl.class.getName() );
    assertThat( reference.getFactoryClassName() ).isEqualTo( OgmSessionFactoryObjectFactory.class.getName() );
    assertThat( reference.get( 0 ) ).isNotNull();
    assertThat( reference.getFactoryClassLocation() ).isNull();

    OgmSessionFactoryObjectFactory objFactory = new OgmSessionFactoryObjectFactory();
    SessionFactory factoryFromRegistry = (SessionFactory) objFactory.getObjectInstance( reference, null, null, null );
    assertThat( factoryFromRegistry.getClass() ).isEqualTo( OgmSessionFactoryImpl.class );
    assertThat( factoryFromRegistry.getReference() ).isEqualTo( factory.getReference() );

    emf.close();
  }
View Full Code Here

  @Rule
  public PackagingRule packaging = new PackagingRule( "persistencexml/jpajtastandalone-options.xml", Refrigerator.class );

  @Test
  public void shouldApplyOptionConfiguratorSpecifiedInPersistenceXml() throws Exception {
    final EntityManagerFactory emf = Persistence.createEntityManagerFactory( "jpajtastandalone-options" );
    OptionsServiceContext optionsContext = getOptionsContext( emf );

    OptionsContext refrigatorOptions = optionsContext.getEntityOptions( Refrigerator.class );
    assertThat( refrigatorOptions.getUnique( ForceExampleOption.class ) ).isTrue();

    OptionsContext microwaveOptions = optionsContext.getEntityOptions( Microwave.class );
    assertThat( microwaveOptions.getUnique( NameExampleOption.class ) ).isEqualTo( "test" );

    OptionsContext temperatureOptions = optionsContext.getPropertyOptions( Refrigerator.class, "temperature" );
    assertThat( temperatureOptions.getUnique( EmbedExampleOption.class ) ).isEqualTo( "Embedded" );

    dropSchemaAndDatabase( emf );
    emf.close();
  }
View Full Code Here

  public static void main(String[] args) {

    TransactionManager tm = getTransactionManager();

    //build the EntityManagerFactory as you would build in in Hibernate Core
    EntityManagerFactory emf = Persistence.createEntityManagerFactory( "ogm-jpa-tutorial" );

    //Persist entities the way you are used to in plain JPA
    try {
      tm.begin();
      logger.infof( "About to store dog and breed" );
      EntityManager em = emf.createEntityManager();
      Breed collie = new Breed();
      collie.setName( "Collie" );
      em.persist( collie );
      Dog dina = new Dog();
      dina.setName( "Dina" );
      dina.setBreed( collie );
      em.persist( dina );
      Long dinaId = dina.getId();
      em.flush();
      em.close();
      tm.commit();

      //Retrieve your entities the way you are used to in plain JPA
      logger.infof( "About to retrieve dog and breed" );
      tm.begin();
      em = emf.createEntityManager();
      dina = em.find( Dog.class, dinaId );
      logger.infof( "Found dog %s of breed %s", dina.getName(), dina.getBreed().getName() );
      em.flush();
      em.close();
      tm.commit();

      emf.close();
    } catch ( Exception e ) {
      e.printStackTrace();
    }

  }
View Full Code Here

      SuperHero.class, HeroClub.class );

  @Test
  public void testJPAPolymorphicCollection() throws Exception {

    final EntityManagerFactory emf = Persistence.createEntityManagerFactory( "jpajtastandalone", TestHelper.getEnvironmentProperties() );

    TransactionManager transactionManager = extractJBossTransactionManager( emf );

    transactionManager.begin();
    final EntityManager em = emf.createEntityManager();
    Hero h = new Hero();
    h.setName( "Spartacus" );
    em.persist( h );
    SuperHero sh = new SuperHero();
    sh.setName( "Batman" );
    sh.setSpecialPower( "Technology and samurai techniques" );
    em.persist( sh );
    HeroClub hc = new HeroClub();
    hc.setName( "My hero club" );
    hc.getMembers().add( h );
    hc.getMembers().add( sh );
    em.persist( hc );
    transactionManager.commit();

    em.clear();

    transactionManager.begin();
    HeroClub lhc = em.find( HeroClub.class, hc.getName() );
    assertThat( lhc ).isNotNull();
    Hero lh = lhc.getMembers().get( 0 );
    assertThat( lh ).isNotNull();
    assertThat( lh ).isInstanceOf( Hero.class );
    Hero lsh = lhc.getMembers().get( 1 );
    assertThat( lsh ).isNotNull();
    assertThat( lsh ).isInstanceOf( SuperHero.class );
    lhc.getMembers().clear();
    em.remove( lh );
    em.remove( lsh );
    em.remove( lhc );

    transactionManager.commit();

    em.close();

    dropSchemaAndDatabase( emf );
    emf.close();
  }
View Full Code Here

  @Rule
  public PackagingRule packaging = new PackagingRule( "persistencexml/jpajtastandalone.xml", CommunityMember.class, Employee.class );

  @Test
  public void testJPAPolymorphicFind() throws Exception {
    final EntityManagerFactory emf = Persistence.createEntityManagerFactory( "jpajtastandalone",
        TestHelper.getEnvironmentProperties() );
    TransactionManager tm = extractJBossTransactionManager( emf );

    tm.begin();
    final EntityManager em = emf.createEntityManager();
    CommunityMember member = new CommunityMember( "Davide" );
    em.persist( member );

    Employee employee = new Employee( "Alex", "EMPLOYER" );
    em.persist( employee );
    tm.commit();

    em.clear();

    tm.begin();
    CommunityMember lh = em.find( CommunityMember.class, member.name );
    assertThat( lh ).isNotNull();
    assertThat( lh ).isInstanceOf( CommunityMember.class );

    CommunityMember lsh = em.find( Employee.class, employee.name );
    assertThat( lsh ).isNotNull();
    assertThat( lsh ).isInstanceOf( Employee.class );
    assertThat( ((Employee) employee).employer ).isEqualTo( employee.employer );

    em.remove( lh );
    em.remove( lsh );
    tm.commit();
    em.close();
    dropSchemaAndDatabase( emf );
    emf.close();
  }
View Full Code Here

  public PackagingRule packaging = new PackagingRule( "persistencexml/jpajtastandalone-datastoreobserver.xml", Noise.class );

  @Test
  public void testObserver() throws Exception {
    try {
      final EntityManagerFactory emf = Persistence.createEntityManagerFactory( "jpajtastandalone-datastoreobserver" );
      Assert.fail( "StartStoppable provider not executed" );
    }
    catch (RuntimeException e) {
      Throwable cause = e;
      do {
View Full Code Here

  @Test
  public void testJTAStandaloneNoOgm() throws Exception {
    // Failure is expected as we didn't configure a JDBC connection nor a Dialect
    // (and this would fail only if effectively loading Hibernate ORM without OGM superpowers)
    error.expect( javax.persistence.PersistenceException.class );
    EntityManagerFactory emf = Persistence.createEntityManagerFactory( "jpajtastandalone-noogm" );
    emf.close(); // should not be reached, but cleanup in case the test fails.
  }
View Full Code Here

    */
   public EntityManager create(Bean<EntityManager> bean, CreationalContext<EntityManager> arg0)
   {
      try
      {
         EntityManagerFactory emf = getEntityManagerFactory();
         EntityManager entityManager = emf.createEntityManager();
         entityManager = getPersistenceProvider(entityManager).proxyEntityManager(entityManager);
         PersistenceContexts persistenceContexts = null;
         try
         {
            persistenceContexts = getPersistenceContexts();
View Full Code Here

TOP

Related Classes of javax.persistence.EntityManagerFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.