Vaadin JPA Container Example
Simple example showing how to use a Vaadin JPA Container
The scenario is a Customer entity in a database. You have a use case which requires the retrieval of a customer record by Name and By Number.
1) Set up a Entity Manager
private final EntityManager em;2) Create the Entity Manager from the persistence unit
em = JPAContainerFactory.createEntityManagerForPersistenceUnit(< name of persistence unit >);
3) Get Customer By Number - returns a JPA Container which is useful for Vaadin Tables
public JPAContainergetCustomerByNumber(String aCustomerNumber) { JPAContainer customer = JPAContainerFactory.make(Customer.class, em); Container.Filter filter = new Compare.Equal("number", aCustomerNumber); customer.addContainerFilter(filter); return customer; }
4) Get Customer By Name - uses JPA named query
public Customer getCustomerByName(String aCustomerName) { Query queryCustomerByName = em.createNamedQuery("Customer.findByName"); queryCustomerByName.setParameter("name", aCustomerName); Customer aCustomer = (Customer) queryCustomerByName.getSingleResult(); return aCustomer; }Note you will need to have a Customer Class to represent your customer entity.
Comments
Post a Comment