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