List of usage examples for javax.persistence EntityManagerFactory createEntityManager
public EntityManager createEntityManager();
EntityManager
. From source file:pt.souplesse.spark.Server.java
public static void main(String[] args) { EntityManagerFactory factory = Persistence.createEntityManagerFactory("guestbook"); EntityManager manager = factory.createEntityManager(); JinqJPAStreamProvider streams = new JinqJPAStreamProvider(factory); get("/messages", (req, rsp) -> { rsp.type("application/json"); return gson.toJson(streams.streamAll(manager, Message.class).collect(Collectors.toList())); });// w ww. j a va 2s. c o m post("/messages", (req, rsp) -> { try { Message msg = gson.fromJson(req.body(), Message.class); if (StringUtils.isBlank(msg.getMessage()) || StringUtils.isBlank(msg.getName())) { halt(400); } manager.getTransaction().begin(); manager.persist(msg); manager.getTransaction().commit(); } catch (JsonSyntaxException e) { halt(400); } rsp.type("application/json"); return gson.toJson(streams.streamAll(manager, Message.class).collect(Collectors.toList())); }); get("/comments", (req, rsp) -> { rsp.type("application/json"); Map<String, List<Body>> body = new HashMap<>(); try (CloseableHttpClient client = create().build()) { String url = String.format("https://api.github.com/repos/%s/events", req.queryMap("repo").value()); log.info(url); body = client.execute(new HttpGet(url), r -> { List<Map<String, Object>> list = gson.fromJson(EntityUtils.toString(r.getEntity()), List.class); Map<String, List<Body>> result = new HashMap<>(); list.stream().filter(m -> m.getOrDefault("type", "").equals("IssueCommentEvent")) .map(m -> new Body(((Map<String, String>) m.get("actor")).get("login"), ((Map<String, Map<String, String>>) m.get("payload")).get("comment") .get("body"))) .forEach(b -> result.compute(b.getLogin(), (k, v) -> v == null ? new ArrayList<>() : Lists.asList(b, v.toArray(new Body[v.size()])))); return result; }); } catch (IOException e) { log.error(null, e); halt(400, e.getMessage()); } return gson.toJson(body); }); }
From source file:edu.harvard.med.iccbl.dev.HibernateConsole.java
public static void main(String[] args) { BufferedReader br = null;// w w w . java 2s . com 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:Professor.java
public static void main(String[] a) throws Exception { JPAUtil util = new JPAUtil(); EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService"); EntityManager em = emf.createEntityManager(); ProfessorService service = new ProfessorService(em); em.getTransaction().begin();/*from w w w . j a va2s. c o m*/ service.findAllProfessors(); util.checkData("select * from Professor"); em.getTransaction().commit(); em.close(); emf.close(); }
From source file:Message.java
public static void main(String[] a) throws Exception { JPAUtil util = new JPAUtil(); EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService"); EntityManager em = emf.createEntityManager(); ProfessorService service = new ProfessorService(em); em.getTransaction().begin();/*from w w w.java2 s.co m*/ service.messageCreateAndList(); util.checkData("select * from Message"); em.getTransaction().commit(); em.close(); emf.close(); }
From source file:com.medicaid.mmis.util.CodeMappingLoader.java
/** * Reads the code mapping xls and inserts any unmapped row to the legacy mapping table. * @throws InvalidFormatException if the input file is not recognized */// w w w. j av a 2s . co m @SuppressWarnings("unchecked") public static void main(String[] args) throws IOException, PortalServiceException, InvalidFormatException { PropertyConfigurator.configure("log4j.properties"); logger = Logger.getLogger(CodeMappingLoader.class); EntityManagerFactory emf = Persistence.createEntityManagerFactory("cms-data-load"); EntityManager em = emf.createEntityManager(); Workbook workbook = WorkbookFactory.create(new File("mapping/CodeMapping.xlsx")); SequenceGeneratorBean sequence = new SequenceGeneratorBean(); sequence.setEm(em); try { em.getTransaction().begin(); List<LegacySystemMapping> rs = em.createQuery("Select l from LegacySystemMapping l").getResultList(); for (LegacySystemMapping mapping : rs) { em.remove(mapping); } importSheet(em, sequence, workbook, "ENROLLMENT_STATUS"); importSheet(em, sequence, workbook, "RISK_LEVEL"); importSheet(em, sequence, workbook, "SPECIALTY_CODE"); importSheet(em, sequence, workbook, "LICENSE_TYPE"); importSheet(em, sequence, workbook, "ISSUING_BOARD"); importSheet(em, sequence, workbook, "PROVIDER_TYPE"); importSheet(em, sequence, workbook, "COUNTY_CODE"); importSheet(em, sequence, workbook, "BEN_OWNER_TYPE"); importSheet(em, sequence, workbook, "LICENSE_STATUS"); importSheet(em, sequence, workbook, "COS"); em.getTransaction().commit(); } catch (Throwable t) { em.getTransaction().rollback(); logger.error("Could not complete import", t); throw new PortalServiceException("Error during import", t); } }
From source file:com.impetus.kvapps.runner.AppRunner.java
/** * main runner method//ww w . ja v a 2s. c om * @param args takes excel data file as input argument args[0]. */ public static void main(String[] args) { // Override CQL version while instantiating entity manager factory. EntityManagerFactory emf = Persistence.createEntityManagerFactory("twissandra,twingo,twirdbms"); EntityManager em = emf.createEntityManager(); try { //populate user set from excel sheet. Set<User> users = UserBroker.brokeUserList(args[0]); for (Iterator<User> iterator = users.iterator(); iterator.hasNext();) { User user = (User) iterator.next(); // on Persist ExecutorService.onPersist(em, user); // on find by id. ExecutorService.findByKey(em, "BigDataUser"); List<User> fetchedUsers = ExecutorService.onQueryByEmail(em, user); if (fetchedUsers != null && fetchedUsers.size() > 0) { logger.info(user.toString()); } logger.info(""); System.out.println("#######################Querying##########################################"); logger.info(""); logger.info(""); } // Execute wild search query. String query = "Select u from User u"; logger.info(query); ExecutorService.findByQuery(em, query); // // Execute native CQL. Fetch tweets for given user. logger.info(""); System.out.println("#######################Querying##########################################"); logger.info(""); logger.info(""); query = "Select * from tweets where user_id='RDBMSUser'"; logger.info(query); ExecutorService.findByNativeQuery(em, query); } finally { onDestroyDBResources(emf, em); } }
From source file:Professor.java
public static void main(String[] a) throws Exception { JPAUtil util = new JPAUtil(); EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService"); EntityManager em = emf.createEntityManager(); ProfessorService service = new ProfessorService(em); em.getTransaction().begin();/*from w w w .j a v a 2 s .c om*/ //service.removeDepartment2(); util.checkData("select * from Professor"); util.checkData("select * from Department"); em.getTransaction().commit(); em.close(); emf.close(); }
From source file:Professor.java
public static void main(String[] a) throws Exception { JPAUtil util = new JPAUtil(); EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService"); EntityManager em = emf.createEntityManager(); ProfessorService service = new ProfessorService(em); em.getTransaction().begin();/* w w w .j ava2s. co m*/ service.lockAllProfessors(); util.checkData("select * from Professor"); em.getTransaction().commit(); em.close(); emf.close(); }
From source file:Professor.java
public static void main(String[] a) throws Exception { JPAUtil util = new JPAUtil(); EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService"); EntityManager em = emf.createEntityManager(); ProfessorService service = new ProfessorService(em); em.getTransaction().begin();/* ww w . j a va 2 s.c o m*/ Professor emp = service.createProfessor("AAA", 45000); em.getTransaction().commit(); System.out.println("Persisted " + emp); util.checkData("select * from Professor"); em.close(); emf.close(); }
From source file:Professor.java
public static void main(String[] a) throws Exception { JPAUtil util = new JPAUtil(); util.executeSQLCommand("CREATE SEQUENCE Emp_Seq;"); EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService"); EntityManager em = emf.createEntityManager(); ProfessorService service = new ProfessorService(em); em.getTransaction().begin();/*from w ww .ja v a 2s . c om*/ Professor emp = service.createProfessor("name", 100); em.getTransaction().commit(); System.out.println("Persisted " + emp); util.checkData("select * from Professor"); em.close(); emf.close(); }