java类javax.persistence.Persistence的实例源码

CidadeTest.java 文件源码 项目:ProjetoFinalInitium 阅读 30 收藏 0 点赞 0 评论 0
@Test
public void selectHqlTest() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("db1start");
    EntityManager manager = factory.createEntityManager();

    Query query = manager.createQuery("Select c from Cidade c");
    List<Cidade> cidades = query.getResultList();

    for (Cidade cidade : cidades) {
        System.out.println(cidade.getNome());
        System.out.println(cidade.getId());
        System.out.println(cidade.getUf().getNome());
    }

}
JPARead.java 文件源码 项目:JPA-Demo 阅读 23 收藏 0 点赞 0 评论 0
public static void main(String[] args) throws Exception {
    // Get the entity manager
    EntityManager em = Persistence.createEntityManagerFactory("company-provider").createEntityManager();
    em.getTransaction().begin();
    em.createNativeQuery("PRAGMA foreign_keys=ON").executeUpdate();
    em.getTransaction().commit();

    // Search in departments by name
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Write the department's name: ");
    String name = reader.readLine();
    System.out.println("Matching departments:");
    Query q1 = em.createNativeQuery("SELECT * FROM departments WHERE name LIKE ?", Department.class);
    q1.setParameter(1, "%" + name + "%");
    List<Department> deps = (List<Department>) q1.getResultList();
    // Print the departments
    for (Department department : deps) {
        System.out.println(department);
    }

    // Get just one department
    // Only use this while looking by unique fields, if not,
    // you could get duplicate results
    System.out.print("Write the department's ID: ");
    int dep_id = Integer.parseInt(reader.readLine());
    Query q2 = em.createNativeQuery("SELECT * FROM departments WHERE id = ?", Department.class);
    q2.setParameter(1, dep_id);
    Department dep = (Department) q2.getSingleResult();
    // Print the department
    System.out.println(dep);

    // Close the entity manager
    em.close();
}
AuthorBuilder.java 文件源码 项目:bibliometrics 阅读 27 收藏 0 点赞 0 评论 0
/**
   * gets an author from the database by determining the type of the provided id. if no author is present it builds one from the id.
   * @param id the author identifier
   * @return the author retrieved from the database or build with the identifier
* @throws JDOMException thrown upon parsing the source response
* @throws IOException thrown upon reading profiles from disc
* @throws SAXException thrown when parsing the files from disc
 */
  public PublicationAuthor retrieveAuthor(String id) throws JDOMException, IOException, SAXException {
      typeOfID = determineID(id);
      LOGGER.info("given ID: " + id + " is of type " + typeOfID);
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("publicationAuthors");
      EntityManager em = emf.createEntityManager();
      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery<PublicationAuthor> q = cb.createQuery(PublicationAuthor.class);
      Root<PublicationAuthor> c = q.from(PublicationAuthor.class);
      List<Predicate> predicates = new ArrayList<>();
      if (typeOfID.equals("surname")) {
          if (id.contains(",")) {
              predicates.add(cb.equal(c.get("surname"),id.substring(0,id.indexOf(","))));
              predicates.add(cb.equal(c.get("firstname"),id.substring(id.indexOf(",")+1)));
              LOGGER.info("retriving surname, firstname from database for " + id);
          } else if (id.contains(" ")) {
              predicates.add(cb.equal(c.get("firstname"),id.substring(0,id.indexOf(" "))));
              predicates.add(cb.equal(c.get("surname"),id.substring(id.indexOf(" ")+1)));
              LOGGER.info("retrieving firstname surname from database for " + id);
          } else {
              predicates.add(cb.equal(c.get("surname"), id));
              LOGGER.info("retrieving surname from database for " + id);
          }
      }
      predicates.add(cb.equal(c.get(typeOfID), id));
      q.select(c).where(cb.equal(c.get(typeOfID), id));
      TypedQuery<PublicationAuthor> query = em.createQuery(q);
      List<PublicationAuthor> authors = query.getResultList();
      em.close();
      if (authors.size() == 1) {
          LOGGER.info("found author in database");
          this.author = authors.get(0);
          return author;
      }
      LOGGER.info("no match in database");
      return buildAuthor(id);
  }
TaskServiceImplTest.java 文件源码 项目:aries-jpa 阅读 25 收藏 0 点赞 0 评论 0
private EntityManagerFactory createTestEMF() {
    Map<String, String> properties = new HashMap<String, String>();
    properties.put("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
    properties.put("javax.persistence.jdbc.url", "jdbc:derby:memory:TEST;create=true");
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("tasklist", properties);
    return emf;
}
TaskServiceImplTest.java 文件源码 项目:aries-jpa 阅读 23 收藏 0 点赞 0 评论 0
private EntityManagerFactory createTestEMF() {
    Map<String, String> properties = new HashMap<String, String>();
    properties.put("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
    properties.put("javax.persistence.jdbc.url", "jdbc:derby:target/test;create=true");
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("tasklist", properties);
    return emf;
}
CidadeTest.java 文件源码 项目:ProjetoFinalInitium 阅读 21 收藏 0 点赞 0 评论 0
@Test
public void updateTest() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("db1start");
    EntityManager manager = factory.createEntityManager();

    Cidade cidade = manager.find(Cidade.class, 2L);
    cidade.setNome("Maringa");
    manager.getTransaction().begin();
    manager.persist(cidade);
    manager.getTransaction().commit();

    factory.close();

}
BasicSample.java 文件源码 项目:spring-data-examples 阅读 22 收藏 0 点赞 0 评论 0
/**
 * Sets up a {@link SimpleJpaRepository} instance.
 */
@Before
public void setUp() {

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa.sample.plain");
    em = factory.createEntityManager();

    userRepository = new SimpleJpaRepository<User, Long>(User.class, em);

    em.getTransaction().begin();
}
EntityManagerFactoryProvider.java 文件源码 项目:cloud-ariba-discovery-rfx-to-external-marketplace-ext 阅读 39 收藏 0 点赞 0 评论 0
private static void initEntityManagerFactory(DataSource dataSource) {
    logger.debug(DEBUG_INITIALIZING_ENTITY_MANAGER_FACTORY);

    Map<Object, Object> properties = new HashMap<>();
    properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, dataSource);

    EntityManagerFactoryProvider.entityManagerFactory = Persistence
            .createEntityManagerFactory(PublicSourcingPersistenceUnit.NAME, properties);

    logger.debug(DEBUG_ENTITY_MANAGER_FACTORY_INITIALIZED);
}
EMFManager.java 文件源码 项目:Pet-Supply-Store 阅读 21 收藏 0 点赞 0 评论 0
/**
 * Get the entity manager factory.
 * @return The entity manager factory.
 */
static EntityManagerFactory getEMF() {
    if (emf == null) {
        HashMap<String, String> persistenceProperties = EMFManager.persistenceProperties;
        if (persistenceProperties == null) {
            persistenceProperties = createPersistencePropertiesFromJavaEnv();
        }
        emf = Persistence.createEntityManagerFactory("tools.descartes.petsupplystore.persistence",
                persistenceProperties);

    }
    return emf;
}
JpaMetamodelRedGProvider.java 文件源码 项目:redg 阅读 29 收藏 0 点赞 0 评论 0
public static JpaMetamodelRedGProvider fromPersistenceUnit(String perstistenceUnitName) {
    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(perstistenceUnitName, properties);

    EntityManager entityManager = entityManagerFactory.createEntityManager();
    return new JpaMetamodelRedGProvider(entityManager.getMetamodel());
}


问题


面经


文章

微信
公众号

扫码关注公众号