javax.persistence.EntityManager.unwrap()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(878)

本文整理了Java中javax.persistence.EntityManager.unwrap()方法的一些代码示例,展示了EntityManager.unwrap()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。EntityManager.unwrap()方法的具体详情如下:
包路径:javax.persistence.EntityManager
类名称:EntityManager
方法名:unwrap

EntityManager.unwrap介绍

[英]Return an object of the specified type to allow access to the provider-specific API. If the provider's EntityManager implementation does not support the specified class, the PersistenceException is thrown.
[中]返回指定类型的对象以允许访问特定于提供程序的API。如果提供程序的EntityManager实现不支持指定的类,则会抛出PersistenceException

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
  public Connection getConnection() {
    if (this.connection == null) {
      this.connection = this.entityManager.unwrap(Connection.class);
    }
    return this.connection;
  }
}

代码示例来源:origin: spring-projects/spring-framework

protected Session getSession(EntityManager entityManager) {
  return entityManager.unwrap(Session.class);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
    throws PersistenceException, SQLException, TransactionException {
  if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
    // Pass custom isolation level on to EclipseLink's DatabaseLogin configuration
    // (since Spring 4.1.2)
    UnitOfWork uow = entityManager.unwrap(UnitOfWork.class);
    uow.getLogin().setTransactionIsolation(definition.getIsolationLevel());
  }
  entityManager.getTransaction().begin();
  if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) {
    // Begin an early transaction to force EclipseLink to get a JDBC Connection
    // so that Spring can manage transactions with JDBC as well as EclipseLink.
    entityManager.unwrap(UnitOfWork.class).beginEarlyTransaction();
  }
  return null;
}

代码示例来源:origin: spring-projects/spring-framework

@Test  // SPR-16956
public void testReadOnly() {
  assertSame(FlushMode.AUTO, sharedEntityManager.unwrap(Session.class).getHibernateFlushMode());
  assertFalse(sharedEntityManager.unwrap(Session.class).isDefaultReadOnly());
  endTransaction();
  this.transactionDefinition.setReadOnly(true);
  startNewTransaction();
  assertSame(FlushMode.MANUAL, sharedEntityManager.unwrap(Session.class).getHibernateFlushMode());
  assertTrue(sharedEntityManager.unwrap(Session.class).isDefaultReadOnly());
}

代码示例来源:origin: hibernate/hibernate-orm

@Before
public void init() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    session.doWork( connection -> {
      try(Statement statement = connection.createStatement(); ) {
        statement.executeUpdate( "ALTER TABLE person ADD COLUMN valid boolean" );
        statement.executeUpdate( "ALTER TABLE person_details ADD COLUMN valid boolean" );
      }
    } );
  });
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void test_hql_api_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::hql-api-example[]
    org.hibernate.query.Query query = session.createQuery(
      "select p " +
      "from Person p " +
      "where p.name like :name"
    );
    //end::hql-api-example[]
  });
}

代码示例来源:origin: hibernate/hibernate-orm

@Before
public void init() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    session.doWork( connection -> {
      try(Statement statement = connection.createStatement(); ) {
        statement.executeUpdate( "ALTER TABLE person ADD COLUMN valid boolean" );
        statement.executeUpdate( "ALTER TABLE Person_phones ADD COLUMN valid boolean" );
      }
    } );
  });
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testCanUnwrapAopProxy() {
  EntityManager em = entityManagerFactory.createEntityManager();
  EntityManager proxy = ProxyFactory.getProxy(EntityManager.class, new SingletonTargetSource(em));
  assertTrue(em instanceof org.hibernate.jpa.HibernateEntityManager);
  assertFalse(proxy instanceof org.hibernate.jpa.HibernateEntityManager);
  assertTrue(proxy.unwrap(org.hibernate.jpa.HibernateEntityManager.class) != null);
  assertSame(em, proxy.unwrap(org.hibernate.jpa.HibernateEntityManager.class));
  assertSame(em.getDelegate(), proxy.getDelegate());
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void test_hql_api_parameter_inferred_type_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::hql-api-parameter-inferred-type-example[]
    org.hibernate.query.Query query = session.createQuery(
      "select p " +
      "from Person p " +
      "where p.name like :name" )
    .setParameter( "name", "J%" );
    //end::hql-api-parameter-inferred-type-example[]
  });
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void test_hql_api_parameter_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::hql-api-parameter-example[]
    org.hibernate.query.Query query = session.createQuery(
      "select p " +
      "from Person p " +
      "where p.name like :name" )
    .setParameter( "name", "J%", StringType.INSTANCE );
    //end::hql-api-parameter-example[]
  });
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void test_hql_api_named_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::hql-api-named-query-example[]
    org.hibernate.query.Query query = session.getNamedQuery( "get_person_by_name" );
    //end::hql-api-named-query-example[]
  });
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  public void shouldUseHibernateOrm52() {
    Session session = entityManager.unwrap( Session.class );

    Kryptonite kryptonite1 = new Kryptonite();
    kryptonite1.id = 1L;
    kryptonite1.description = "Some Kryptonite";
    session.persist( kryptonite1 );

    // EntityManager methods exposed through Session only as of 5.2
    Kryptonite loaded = session.find( Kryptonite.class, 1L );

    assertThat( loaded.description, equalTo( "Some Kryptonite" ) );
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void test_sql_hibernate_scalar_named_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-scalar-named-query-example[]
    List<String> names = session.getNamedQuery(
      "find_person_name" )
    .list();
    //end::sql-hibernate-scalar-named-query-example[]
    assertEquals(3, names.size());
  });
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void test_sql_hibernate_query_scalar_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-all-columns-scalar-query-example[]
    List<Object[]> persons = session.createNativeQuery(
      "SELECT * FROM Person" )
    .list();
    //end::sql-hibernate-all-columns-scalar-query-example[]
    assertEquals(3, persons.size());
  });
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void test_sql_hibernate_multiple_scalar_values_dto_named_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-multiple-scalar-values-dto-named-query-example[]
    List<PersonNames> personNames = session.getNamedQuery(
      "find_person_name_and_nickName_dto" )
    .list();
    //end::sql-hibernate-multiple-scalar-values-dto-named-query-example[]
    assertEquals(3, personNames.size());
  });
}

代码示例来源:origin: hibernate/hibernate-orm

private Integer getCurrentAuditUniqueGroupId() {
  return TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
    final Session session = entityManager.unwrap( Session.class );
    final Query query = session.createSQLQuery(
        "SELECT uniqueGroup_id FROM GroupMember_AUD ORDER BY rev DESC" ).addScalar(
        "uniqueGroup_id",
        IntegerType.INSTANCE
    ).setMaxResults( 1 );
    final Object result = query.getSingleResult();
    assertNotNull( result );
    return (Integer) result;
  } );
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void updateDetachedEntity() {
  Customer customer = doInJPA( this::entityManagerFactory, entityManager -> {
    return entityManager.find( Customer.class, customerId );
  } );
  assertModifiedAtWasNotUpdated( customer );
  doInJPA( this::entityManagerFactory, entityManager -> {
    entityManager.unwrap( Session.class ).update( customer );
  } );
  doInJPA( this::entityManagerFactory, entityManager -> {
    assertModifiedAtWasUpdated( entityManager.find( Customer.class, customerId ) );
  } );
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void test_sql_hibernate_entity_named_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-entity-named-query-example[]
    List<Person> persons = session.getNamedQuery(
      "find_person_by_name" )
    .setParameter("name", "J%")
    .list();
    //end::sql-hibernate-entity-named-query-example[]
    assertEquals(1, persons.size());
  });
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void test_sql_hibernate_entity_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-entity-query-example[]
    List<Person> persons = session.createNativeQuery(
      "SELECT * FROM Person" )
    .addEntity( Person.class )
    .list();
    //end::sql-hibernate-entity-query-example[]
    assertEquals(3, persons.size());
  });
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testHibernateProcedureCallReturnValueParameter() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    //tag::sql-hibernate-call-sp-no-out-mysql-example[]
    Session session = entityManager.unwrap( Session.class );
    ProcedureCall call = session.createStoredProcedureCall( "sp_phones" );
    call.registerParameter( 1, Long.class, ParameterMode.IN ).bindValue( 1L );
    Output output = call.getOutputs().getCurrent();
    List<Object[]> personComments = ( (ResultSetOutput) output ).getResultList();
    //end::sql-hibernate-call-sp-no-out-mysql-example[]
    assertEquals( 2, personComments.size() );
  });
}

相关文章