Example usage for javax.persistence EntityManager getDelegate

List of usage examples for javax.persistence EntityManager getDelegate

Introduction

In this page you can find the example usage for javax.persistence EntityManager getDelegate.

Prototype

public Object getDelegate();

Source Link

Document

Return the underlying provider object for the EntityManager, if available.

Usage

From source file:edu.harvard.med.iccbl.dev.HibernateConsole.java

public static void main(String[] args) {
    BufferedReader br = null;/* w ww .j  a  va 2s .c  o m*/
    try {
        CommandLineApplication app = new CommandLineApplication(args);
        app.processOptions(true, false);
        br = new BufferedReader(new InputStreamReader(System.in));

        EntityManagerFactory emf = (EntityManagerFactory) app.getSpringBean("entityManagerFactory");
        EntityManager em = emf.createEntityManager();

        do {
            System.out.println("Enter HQL query (blank to quit): ");
            String input = br.readLine();
            if (input.length() == 0) {
                System.out.println("Goodbye!");
                System.exit(0);
            }
            try {
                List list = ((Session) em.getDelegate()).createQuery(input).list(); // note: this uses the Hibernate Session object, to allow HQL (and JPQL)
                // List list = em.createQuery(input).getResultList();  // note: this JPA method supports JPQL

                System.out.println("Result:");
                for (Iterator iter = list.iterator(); iter.hasNext();) {
                    Object item = iter.next();
                    // format output from multi-item selects ("select a, b, c, ... from ...")
                    if (item instanceof Object[]) {
                        List<Object> fields = Arrays.asList((Object[]) item);
                        System.out.println(StringUtils.makeListString(fields, ", "));
                    }
                    // format output from single-item selected ("select a from ..." or "from ...")
                    else {
                        System.out.println("[" + item.getClass().getName() + "]: " + item);
                    }
                }
                System.out.println("(" + list.size() + " rows)\n");
            } catch (Exception e) {
                System.out.println("Hibernate Error: " + e.getMessage());
                log.error("Hibernate error", e);
            }
            System.out.println();
        } while (true);
    } catch (Exception e) {
        System.err.println("Fatal Error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(br);
    }
}

From source file:cn.guoyukun.spring.jpa.repository.hibernate.HibernateUtils.java

/**
 * ?jpa EntityManager ? hibernate Session API
 *
 * @param em/*from  w  w  w .  j  av  a 2  s  . c om*/
 * @return
 */
public static Session getSession(EntityManager em) {
    return (Session) em.getDelegate();
}

From source file:com.hmsinc.epicenter.model.util.ModelUtils.java

/**
 * Creates a cacheable native Hibernate query.
 * /*from  www. ja  v a  2  s . c  o  m*/
 * @param entityManager
 * @param query
 * @return
 */
public static org.hibernate.Query nativeQuery(final EntityManager entityManager, final String query) {
    return ((Session) entityManager.getDelegate()).createQuery(query).setCacheable(true);
}

From source file:com.hmsinc.epicenter.model.util.ModelUtils.java

/**
 * Creates a native SQL query./*  www . j  a  v a2s. c o  m*/
 * 
 * @param entityManager
 * @param query
 * @return
 */
public static SQLQuery sqlQuery(final EntityManager entityManager, final String query) {
    return ((Session) entityManager.getDelegate()).createSQLQuery(query);
}

From source file:com.hmsinc.epicenter.model.util.ModelUtils.java

/**
 * Disables "nested loops" optimization on PostgreSQL. This is needed with
 * various spatial queries that confuse the optimizer.
 * /* w w  w . j a  va2 s. c  om*/
 * @param entityManager
 */
public static void disableNestedLoops(final EntityManager entityManager) {

    final Session s = (Session) entityManager.getDelegate();
    final SessionFactoryImpl sf = (SessionFactoryImpl) s.getSessionFactory();
    if (sf.getDialect() instanceof PostgreSQLDialect) {
        s.doWork(new Work() {
            public void execute(Connection conn) throws SQLException {
                conn.createStatement().execute("set enable_nestloop=off");
            }

        });
    }
}

From source file:com.hmsinc.epicenter.model.util.ModelUtils.java

/**
 * Enables "nested loops" optimization on PostgreSQL. This is needed with
 * various spatial queries that confuse the optimizer.
 * /*from  w w w.j  a  v a2  s.c om*/
 * @param entityManager
 */
public static void enableNestedLoops(final EntityManager entityManager) {

    final Session s = (Session) entityManager.getDelegate();
    final SessionFactoryImpl sf = (SessionFactoryImpl) s.getSessionFactory();
    if (sf.getDialect() instanceof PostgreSQLDialect) {
        s.doWork(new Work() {
            public void execute(Connection conn) throws SQLException {
                conn.createStatement().execute("set enable_nestloop=on");
            }

        });
    }
}

From source file:com.hmsinc.epicenter.model.util.ModelUtils.java

/**
 * Creates a cacheable native Hibernate named query.
 * /*w ww .  j  av a2s  .com*/
 * @param entityManager
 * @param namedQuery
 * @return
 */
public static org.hibernate.Query nativeNamedQuery(final EntityManager entityManager, final String namedQuery) {
    return ((Session) entityManager.getDelegate()).getNamedQuery(namedQuery).setCacheable(true);
}

From source file:com.hmsinc.epicenter.model.util.ModelUtils.java

/**
 * Creates a native hibernate Criteria query.
 * // w  w  w.  j av  a  2s  . co m
 * @param entityManager
 * @param persistentClass
 * @return
 */
public static Criteria criteriaQuery(final EntityManager entityManager, final Class<?> persistentClass) {
    return ((Session) entityManager.getDelegate()).createCriteria(persistentClass).setCacheable(true);
}

From source file:com.hmsinc.epicenter.model.util.ModelUtils.java

/**
 * Creates a native hibernate Criteria query with an alias.
 * /* ww w  . j  a v  a 2  s .  c om*/
 * @param entityManager
 * @param persistentClass
 * @param alias
 * @return
 */
public static Criteria criteriaQuery(final EntityManager entityManager, final Class<?> persistentClass,
        final String alias) {
    return ((Session) entityManager.getDelegate()).createCriteria(persistentClass, alias).setCacheable(true);
}

From source file:com.impetus.kvapps.runner.ExecutorService.java

/**
 * On find by native CQL3 query.//w w  w.j  a v a 2  s .  c om
 * 
 * @param em            entity manager instance.
 * @param query         native cql3 query.
 */
@SuppressWarnings("unchecked")
static void findByNativeQuery(final EntityManager em, final String query) {

    Query q = em.createNativeQuery(query, Tweets.class);

    Map<String, Client> clients = (Map<String, Client>) em.getDelegate();
    ThriftClient client = (ThriftClient) clients.get("twissandra");
    client.setCqlVersion(CassandraConstants.CQL_VERSION_3_0);

    logger.info("[On Find Tweets by CQL3]");
    List<Tweets> tweets = q.getResultList();

    System.out.println("#######################START##########################################");
    logger.info("\t\t User's total tweets:" + tweets.size());
    onPrintTweets(tweets);
    logger.info("\n");
    // logger.info("First tweet:" users.get(0).getTweets().);
    System.out.println("#######################END############################################");
    logger.info("\n");
}