JPAContainer Help

Hi,

I’m new to Vaadin and have only used it only for a few days. I was able to mockup a few application form in only a few days. Now, I am attempting to bind the data from a MySQL table to the application form using JPAContainer.


JPAContainer container = JPAContainerFactory.make(
EntityAccount.class, “Account”);

With this code, I am getting the following error.


the type javax.persistence.EntityManager cannot be resolved. It is indirectly referenced from required .class files

I also noticed the javax annotations are also not imported correctly on an entity bean I created.

I am using Eclipse Luna as the java IDE and not sure if it is a setup issue or something else. If someone know of a solution to resolve this error, I would greatly appreciate the feedback.

Thanks.

Further…I pointed the project to the jdk.8.0_25 rather than jre thinking it would resolve the issue and it did not.

Can you post your persistence.xml file?

Hi darrelln09,

Looking at the project layout, I can’t locate the persistence.xml file. Can you please point me in the right direction?

Thanks.

It should be inside a folder named META-INF.

There is NO persistence.xml in the META-INF folder for my project.

Thanks.

Well that certainly explains your error!

i suggest you page through the JPAContainer Tutorial here: https://vaadin.com/directory#!addon/vaadin-jpacontainer

You should also make sure your project setup in eclipse includes the JPA facet. There are several nice tools in that package to help annotate the persistence entity classes, edit the persistence.xml file, and forward- or reverse-engineer the associated database tables.

I thought my adding the ivy dependency to ivy.xml the eclipse will download addon.

Thanks for the help! I will give it a short.

Adding the JPAContainer dependency in Ivy will definitely download the required jar files from Vaadin. That’s all you need to do if you choose to annotate all your entity classes and build the persistence.xml file by hand. Adding the JPA Facet in eclipse will help automate those tasks. I also recommend using EclipseLink as your persistence provider. It works well with the JPA Facet.

Thanks darrelln09! I have both EclipseLink and JPA installed and configured. One more question, how do I activate JPA to my Vaadin project. I have already setup the persistence.xml using JPA perspective.

Thanks for all your help!

The last step depends on what server you are using. If it’s Tomcat, you probably just need to add the javax.persistence and eclipselink dependencies into Ivy so they will deploy to the server in your war file. If you’re using a Java EE server such as Wildfly you can install those as server modules so they are always available.

I am using Tomcat 8. Ok…I’ll give it a try.

Thanks.

Thank you darrelln09, everything is working as expected. One final question, I used JPA entity from table to create a bean object, what is the call out mechanism to pull data from the database to the bean object?

Thank.

I was able to pull data from JPA into an entity object.

EntityManagerFactory emFactory = Persistence.createEntityManagerFactory(“MyApp”);

em = emFactory.createEntityManager();
account = em.find(EntityAccount.class, new Long(6));
this._idField.setValue(String.valueOf(account.getId()));

Thanks for all your help!

Hi Allen,

I’m glad you made it work and bit sad that I’m bit too late in this thread. If you are starting with Vaadin and JPA, my suggestion is
don’t use JPAContainer
. Instead suggest to create a facade/service via which you fetch your data with JPA and use plain java.util.List + entity beans. And use Java EE or Spring as a basis to make it all simpler. Check e.g.
this example
for a good start for simple JPA application.

cheers,
matti

Thanks Matti, let me read the tutorial and catch up with you soon :-).

Hi Matti,

The design approach you recommended is fabulous and I am still learning how to use it. However, I am not sure if I am doing it correctly. Here’s my attempt to create a JPA service. Can you tell me if I am doing it correctly and if you have any recomendations to improve it. Thanks.

package aaptimize.account;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import aaptimize.domain.EntityAccount;

public class AccountService {

    private static final String PERSISTENCE_UNIT_NAME = "AAPTimize";
    @PersistenceContext(unitName = "AccountService")
    protected EntityManagerFactory factory;
    protected EntityManager em;

    public AccountService() {
        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

        em = factory.createEntityManager();
    }
    
    public EntityManager getEntityManager() {
        return em;
    }
    
    public EntityTransaction getTransaction() {
        return em.getTransaction();
    }

    public EntityAccount create(EntityAccount account) {
        if (account == null) {
            em.persist(account);
        } else {
            em.merge(account);
        }

        return account;
    }

    public void remove(long id) {
        final EntityAccount account = findAccount(id);

        if (account != null) {
            em.remove(account);
        }
    }
    
    public void remove(EntityAccount account) {
        if (account != null) {
            em.remove(account);
        }
    }
    
    public void refresh(EntityAccount account) {
        if (account != null) {
            em.refresh(account);
        }
    }

    public EntityAccount findAccount(long id) {
        return em.find(EntityAccount.class, id);
    }

    public EntityAccount findAccount(EntityAccount account) {
        return em.find(EntityAccount.class, account.getId());
    }

    @SuppressWarnings("unchecked")
    public Collection<EntityAccount> findByAccountId(long id) {
        final Query query = em
                .createQuery("SELECT e FROM EntityAccount e"
                        + " where e.entityAccount.id = :accountId",
                        EntityAccount.class);
        query.setParameter("accountId", 1);

        return (Collection<EntityAccount>) query.getResultList();
    }

    @SuppressWarnings("unchecked")
    public Collection<EntityAccount> findAll() {
        final Query query = em.createQuery("SELECT e FROM EntityAccount e");

        return (Collection<EntityAccount>) query.getResultList();
    }
    
    public void close() {
        em.close();
    }
}

Hi,

That looks pretty ok, but I’m not sure how you are planning to deal with transaction management? Even just for the sake of transaction management I’d use either Spring (Data) or a stateless EJB (in case you have a Java EE server). With them and container managed persistence context, you’ll get that pretty much for free. Which server are you using? If its a Java EE 6+ compatible, then I’d go with a local EBJ (+ most likely a DeltaSpike Data) else I’d throw in Spring to build the service like in e.g.
this example
.

cheers,
matti

Hi Matti,

I am using Tomcat 8 as my server. I’ll read up on your example and thanks for your helpful and awesome suggestions.