Java Code Examples for javax.persistence.EntityManager#getReference()

The following examples show how to use javax.persistence.EntityManager#getReference() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpdateDao.java    From peer-os with Apache License 2.0 7 votes vote down vote up
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();
    }
}
 
Example 2
Source File: JpaTest.java    From code with Apache License 2.0 6 votes vote down vote up
/**
 * 查询策略:使用延迟加载策略
 *  只有使用对象时才会执行查询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();
    }
}
 
Example 3
Source File: JPATransactionManagerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 4
Source File: BaseJPARepository.java    From J-Kinopoisk2IMDB with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Transactional
@Override
public void delete(ID id) {
    EntityManager entityManager = entityManagerProvider.get();

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

    entityManager.remove(entity);
}
 
Example 5
Source File: JPACodeDataProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: JtaEntityManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 7
Source File: ScopeAdapter.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static ScopeEntity toEntity(EntityManager em, Scope scope) {
    if (scope instanceof ScopeAdapter) {
        return ((ScopeAdapter)scope).getEntity();
    } else {
        return em.getReference(ScopeEntity.class, scope.getId());
    }
}
 
Example 8
Source File: PermissionTicketAdapter.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static PermissionTicketEntity toEntity(EntityManager em, PermissionTicket permission) {
    if (permission instanceof PermissionTicketAdapter) {
        return ((PermissionTicketAdapter)permission).getEntity();
    } else {
        return em.getReference(PermissionTicketEntity.class, permission.getId());
    }
}
 
Example 9
Source File: PolicyAdapter.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static PolicyEntity toEntity(EntityManager em, Policy policy) {
    if (policy instanceof PolicyAdapter) {
        return ((PolicyAdapter)policy).getEntity();
    } else {
        return em.getReference(PolicyEntity.class, policy.getId());
    }
}
 
Example 10
Source File: ResourceAdapter.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static ResourceEntity toEntity(EntityManager em, Resource resource) {
    if (resource instanceof ResourceAdapter) {
        return ((ResourceAdapter)resource).getEntity();
    } else {
        return em.getReference(ResourceEntity.class, resource.getId());
    }
}
 
Example 11
Source File: ResourceServerAdapter.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static ResourceServerEntity toEntity(EntityManager em, ResourceServer resource) {
    if (resource instanceof ResourceAdapter) {
        return ((ResourceServerAdapter)resource).getEntity();
    } else {
        return em.getReference(ResourceServerEntity.class, resource.getId());
    }
}
 
Example 12
Source File: NonstrictReadwriteTest.java    From hibernate4-memcached with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 13
Source File: RoleAdapter.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public static RoleEntity toRoleEntity(RoleModel model, EntityManager em) {
    if (model instanceof RoleAdapter) {
        return ((RoleAdapter) model).getEntity();
    }
    return em.getReference(RoleEntity.class, model.getId());
}
 
Example 14
Source File: GroupAdapter.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public static GroupEntity toEntity(GroupModel model, EntityManager em) {
    if (model instanceof GroupAdapter) {
        return ((GroupAdapter)model).getEntity();
    }
    return em.getReference(GroupEntity.class, model.getId());
}
 
Example 15
Source File: ClientScopeAdapter.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public static ClientScopeEntity toClientScopeEntity(ClientScopeModel model, EntityManager em) {
    if (model instanceof ClientScopeAdapter) {
        return ((ClientScopeAdapter)model).getEntity();
    }
    return em.getReference(ClientScopeEntity.class, model.getId());
}