Search in sources :

Example 1 with Customer

use of com.company.app.model.Customer in project microservices by pwillhan.

the class CustomerVerticle method addOne.

private void addOne(RoutingContext context) {
    final Customer customer = Json.decodeValue(context.getBodyAsString(), Customer.class);
    entityManager.getTransaction().begin();
    CustomerJpaDao customerDao = new CustomerJpaDao();
    customerDao.setEntityManager(entityManager);
    Customer createdCustomer = customerDao.save(customer);
    entityManager.getTransaction().commit();
    context.response().setStatusCode(201).putHeader("content-type", "application/json").end(Json.encodePrettily(createdCustomer));
}
Also used : Customer(com.company.app.model.Customer) CustomerJpaDao(com.company.app.dao.CustomerJpaDao)

Example 2 with Customer

use of com.company.app.model.Customer in project microservices by pwillhan.

the class MySqlApp method main.

public static void main(String[] args) {
    EntityManagerFactory entityManagerFactory = null;
    EntityManager entityManager = null;
    try {
        entityManagerFactory = Persistence.createEntityManagerFactory("test-unit");
        entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        CustomerJpaDao customerDao = new CustomerJpaDao();
        customerDao.setEntityManager(entityManager);
        Customer customer = new Customer();
        customer.setName("Tom");
        customer.setAge(18);
        // CREATE
        Customer createdCustomer = customerDao.save(customer);
        System.out.println(createdCustomer.getId());
        // GET one
        Long customerId = createdCustomer.getId();
        Customer foundCustomer = customerDao.findById(customerId);
        System.out.println(foundCustomer);
        // UPDATE
        foundCustomer.setName("Dick");
        Customer updatedCustomer = customerDao.save(foundCustomer);
        System.out.println(updatedCustomer);
        entityManager.getTransaction().commit();
        // Native Query
        nativeQuery(entityManager);
    } catch (Exception e) {
        entityManager.getTransaction().rollback();
    } finally {
        entityManager.close();
        entityManagerFactory.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Customer(com.company.app.model.Customer) EntityManagerFactory(javax.persistence.EntityManagerFactory) CustomerJpaDao(com.company.app.dao.CustomerJpaDao) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with Customer

use of com.company.app.model.Customer in project microservices by pwillhan.

the class CustomerVerticle method getOne.

private void getOne(RoutingContext routingContext) {
    final String id = routingContext.request().getParam("id");
    if (id == null) {
        routingContext.response().setStatusCode(400).end();
    } else {
        final Long idAsLong = Long.valueOf(id);
        entityManager.getTransaction().begin();
        CustomerJpaDao customerDao = new CustomerJpaDao();
        customerDao.setEntityManager(entityManager);
        Customer customer = customerDao.findById(idAsLong);
        entityManager.getTransaction().commit();
        if (customer == null) {
            routingContext.response().setStatusCode(404).end();
        } else {
            routingContext.response().putHeader("content-type", "application/json; charset=utf-8").end(Json.encodePrettily(customer));
        }
    }
}
Also used : Customer(com.company.app.model.Customer) CustomerJpaDao(com.company.app.dao.CustomerJpaDao)

Aggregations

CustomerJpaDao (com.company.app.dao.CustomerJpaDao)3 Customer (com.company.app.model.Customer)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1