javax.persistence.EntityManager#getReference ( )源码实例Demo

下面列出了javax.persistence.EntityManager#getReference ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: code   文件: JpaTest.java
/**
 * 查询策略:使用延迟加载策略
 *  只有使用对象时才会执行查询sql语句:先输出true,再打印sql
 */
@Test
public void testLoadOne() {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        // 1、获取EntityManager
        em = JpaUtil.getEntityManager();
        // 2、获取事务
        tx = em.getTransaction();
        // 3、开启事务
        tx.begin();
        // 4、获取数据
        // primaryKey类型必须和实体主键类型一致,否则查询不到
        Customer c1 = em.getReference(Customer.class, 2L);
        Customer c2 = em.getReference(Customer.class, 2L);
        System.out.println(c1 == c2);
        // 5、提交事务
        tx.commit();
        //System.out.println(c1);
    } catch (Exception e) {
        if (tx != null) tx.rollback();
    } finally {
        if (em != null) em.close();
    }
}
 
源代码2 项目: peer-os   文件: UpdateDao.java
public void remove( final Long id )
{
    EntityManager em = emf.createEntityManager();
    try
    {
        em.getTransaction().begin();
        UpdateEntity item = em.getReference( UpdateEntity.class, id );
        em.remove( item );
        em.getTransaction().commit();
    }
    catch ( Exception e )
    {
        LOG.error( e.toString(), e );
        if ( em.getTransaction().isActive() )
        {
            em.getTransaction().rollback();
        }
    }
    finally
    {
        em.close();
    }
}
 
@Test
public void testJpaTransactionManagerRouteRoute() throws Exception {

    CamelContext camelctx = contextRegistry.getCamelContext("jpa-context");
    Assert.assertNotNull("Expected jpa-context to not be null", camelctx);

    // Persist a new account entity
    Account account = new Account(1, 500);
    camelctx.createProducerTemplate().sendBody("direct:start", account);

    JpaComponent component = camelctx.getComponent("jpa", JpaComponent.class);
    EntityManagerFactory entityManagerFactory = component.getEntityManagerFactory();

    // Read the saved entity back from the database
    EntityManager em = entityManagerFactory.createEntityManager();
    Account result = em.getReference(Account.class, 1);
    Assert.assertEquals(account, result);
}
 
源代码4 项目: J-Kinopoisk2IMDB   文件: BaseJPARepository.java
/**
 * {@inheritDoc}
 */
@Transactional
@Override
public void delete(ID id) {
    EntityManager entityManager = entityManagerProvider.get();

    T entity = entityManager.getReference(getType(), id);

    entityManager.remove(entity);
}
 
源代码5 项目: cxf   文件: JPACodeDataProvider.java
private ServerAuthorizationCodeGrant removeCodeGrant(String code, EntityManager em) throws OAuthServiceException {
    ServerAuthorizationCodeGrant grant = em.getReference(ServerAuthorizationCodeGrant.class, code);
    try {
        em.remove(grant);
    } catch (EntityNotFoundException e) {
    }
    return grant;
}
 
源代码6 项目: tomee   文件: JtaEntityManager.java
public <T> T getReference(final Class<T> entityClass, final Object primaryKey) {
    final EntityManager entityManager = getEntityManager();
    try {
        final Timer timer = Op.getReference.start(this.timer, this);
        try {
            return entityManager.getReference(entityClass, primaryKey);
        } finally {
            timer.stop();
        }
    } finally {
        closeIfNoTx(entityManager);
    }
}
 
源代码7 项目: keycloak   文件: ScopeAdapter.java
public static ScopeEntity toEntity(EntityManager em, Scope scope) {
    if (scope instanceof ScopeAdapter) {
        return ((ScopeAdapter)scope).getEntity();
    } else {
        return em.getReference(ScopeEntity.class, scope.getId());
    }
}
 
源代码8 项目: keycloak   文件: PermissionTicketAdapter.java
public static PermissionTicketEntity toEntity(EntityManager em, PermissionTicket permission) {
    if (permission instanceof PermissionTicketAdapter) {
        return ((PermissionTicketAdapter)permission).getEntity();
    } else {
        return em.getReference(PermissionTicketEntity.class, permission.getId());
    }
}
 
源代码9 项目: keycloak   文件: PolicyAdapter.java
public static PolicyEntity toEntity(EntityManager em, Policy policy) {
    if (policy instanceof PolicyAdapter) {
        return ((PolicyAdapter)policy).getEntity();
    } else {
        return em.getReference(PolicyEntity.class, policy.getId());
    }
}
 
源代码10 项目: keycloak   文件: ResourceAdapter.java
public static ResourceEntity toEntity(EntityManager em, Resource resource) {
    if (resource instanceof ResourceAdapter) {
        return ((ResourceAdapter)resource).getEntity();
    } else {
        return em.getReference(ResourceEntity.class, resource.getId());
    }
}
 
源代码11 项目: keycloak   文件: ResourceServerAdapter.java
public static ResourceServerEntity toEntity(EntityManager em, ResourceServer resource) {
    if (resource instanceof ResourceAdapter) {
        return ((ResourceServerAdapter)resource).getEntity();
    } else {
        return em.getReference(ResourceServerEntity.class, resource.getId());
    }
}
 
@Test
public void nonstrictReadWrite() throws Exception {
    log.debug("#### First load");
    EntityManager em = EntityTestUtils.start();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();

    log.debug("Author 1 first load.");
    Author author1 = em.find(Author.class, 1L);
    log.debug("Author first load : {}", author1);
    transaction.commit();
    EntityTestUtils.stop(em);

    log.debug("#### Cached Load");
    em = EntityTestUtils.start();
    transaction = em.getTransaction();
    transaction.begin();
    author1 = em.find(Author.class, 1L);
    log.debug("Author 1 reload : {}", author1);
    transaction.commit();
    EntityTestUtils.stop(em);

    log.debug("#### Insert!");
    em = EntityTestUtils.start();
    transaction = em.getTransaction();
    transaction.begin();

    Author newAuthor = new Author();
    newAuthor.setName("Some one famous");
    newAuthor.setCountry("Some where over the rainbow");

    em.persist(newAuthor);
    log.debug("new Author inserted : {}", newAuthor);

    transaction.commit();
    EntityTestUtils.stop(em);

    log.debug("### Update!");
    em = EntityTestUtils.start();
    transaction = em.getTransaction();
    transaction.begin();

    Author authorToBeUpdated = em.find(Author.class, 1L);
    authorToBeUpdated.setName(authorToBeUpdated.getName() + " Postfix");
    em.merge(authorToBeUpdated);
    transaction.commit();
    EntityTestUtils.stop(em);

    log.debug("### Delete!");
    em = EntityTestUtils.start();
    transaction = em.getTransaction();
    transaction.begin();

    Author authorToBeDeleted = em.getReference(Author.class, newAuthor.getId());
    em.remove(authorToBeDeleted);
    transaction.commit();
    EntityTestUtils.stop(em);
}
 
源代码13 项目: keycloak   文件: RoleAdapter.java
public static RoleEntity toRoleEntity(RoleModel model, EntityManager em) {
    if (model instanceof RoleAdapter) {
        return ((RoleAdapter) model).getEntity();
    }
    return em.getReference(RoleEntity.class, model.getId());
}
 
源代码14 项目: keycloak   文件: GroupAdapter.java
public static GroupEntity toEntity(GroupModel model, EntityManager em) {
    if (model instanceof GroupAdapter) {
        return ((GroupAdapter)model).getEntity();
    }
    return em.getReference(GroupEntity.class, model.getId());
}
 
源代码15 项目: keycloak   文件: ClientScopeAdapter.java
public static ClientScopeEntity toClientScopeEntity(ClientScopeModel model, EntityManager em) {
    if (model instanceof ClientScopeAdapter) {
        return ((ClientScopeAdapter)model).getEntity();
    }
    return em.getReference(ClientScopeEntity.class, model.getId());
}