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));
}
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();
}
}
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));
}
}
}
Aggregations