3

im using vaadin to develop a UI for my application running hibernate 4.3.1 What I'm trying to do is bind the data to the vaadin JPAcontainer then use it in the Grid component to allow lazy-loading. But when I try and create the EntityManager it throws the following error

java.lang.ClassCastException: org.hibernate.ejb.HibernatePersistence cannot be cast to javax.persistence.spi.PersistenceProvider
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at com.vaadin.addon.jpacontainer.JPAContainerFactory.createEntityManagerForPersistenceUnit(JPAContainerFactory.java:122)
at com.vaadin.addon.jpacontainer.JPAContainerFactory.make(JPAContainerFactory.java:105)

I used the hibernate-jpa-2.1-api-1.0.0.Final.jar as well as the persistence-api-1.0.2.jar to import my EnityManager but I keep getting the same error

here is the class where I create the EntityManager

 public class workManager{
    public static void create() {
    DataAccess dao= new DataAccess();

    EntityManager em = Persistence  //error here
            .createEntityManagerFactory("myUI")
            .createEntityManager();

    em.getTransaction().begin();
    dao.init();
    List<work> list = dao.findAll(); //get all rows in table
    em.persist(list);
    dao.close();
    em.getTransaction().commit();
}

persistence.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
 <persistence-unit name="myProject">
    <!-- Specify the JPA provider to use -->
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <!-- Entity beans go here -->
    <class>com.dao.model.Work</class>


    <properties>
        <!-- DB connection properties -->
        <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://S12345:1111;databaseName=dbName" />
        <!-- property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://L71111:1111;databaseName=dbName" / -->
        <property name="javax.persistence.jdbc.user" value="iaSys" />
        <property name="javax.persistence.jdbc.password" value="password" />
        <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />

        <!-- Settings for c3p0 connection pooling -->
        <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
        <property name="hibernate.c3p0.max_size" value="100" />
        <property name="hibernate.c3p0.min_size" value="0" />
        <property name="hibernate.c3p0.acquire_increment" value="1" />
        <property name="hibernate.c3p0.idle_test_period" value="300" />
        <property name="hibernate.c3p0.max_statements" value="0" />
        <property name="hibernate.c3p0.timeout" value="100" />
    </properties>
</persistence-unit>

in another class I create the JPAContainer

 private JPAContainer<work> container = JPAContainerFactory.make(work.class,
            JpaProjectUI.PERSISTENCE_UNIT);
 grid.setContainerDataSource(container);

and here is where my EntityManager is called

public class JpaPojectUI extends UI {
private static final long serialVersionUID = 1L;
public static final String PERSISTENCE_UNIT = "myProject";

static {
    workManager.create();
}

@Override
protected void init(VaadinRequest request) {
    setContent(new mainUI());
}

I was following the AddressBook demo that came with the JPAContainer add-on

I'm using Tomcat 8 to run my application

does anyone have an idea why this is happening? please feel free to ask me for more info if the problem isn't clear!

Here are the jars in my WEB-INF folder

enter image description here

I also added another project to my build path that has the following list of jars

enter image description here

thanks

Reda
  • 470
  • 2
  • 11
  • 19
  • What all jars there in your classpath (/WEB-INF/lib)? There might be different versions of same jar, hence the conflict. – RP- Jul 13 '15 at 23:46
  • Hello RP, I updated the description with a list of jars in my build path – Reda Jul 14 '15 at 17:35

1 Answers1

6

Please remove persistence-api-1.0.2.jar from your web application and see the results. There is a conflict between hibernate-jpa and persistance-api

Paweł Głowacz
  • 2,926
  • 3
  • 18
  • 25
  • I removed it, cleaned my project then ran i again but I still got the same error – Reda Jul 14 '15 at 17:36
  • 1
    I see also `vaadin-jpacontainer.jar` and `vaadin-lazyquerycontainer.jar` Could you delete these jars also and check results? – Paweł Głowacz Jul 14 '15 at 17:46
  • 1
    I think you need to decide what jpa container are you going to use. Vaadin or Hibernate one. Propably there is conflict. – Paweł Głowacz Jul 14 '15 at 17:49
  • Yep! I removed the lazyquerycontainer and kept the hibernate and jpacontainer jars and got rid of the error. Now I got a new one saying "No Persistence provider for EntityManager named myProject" is it could it be because I used the persistence.xml from another project and only changed the name to myProject? – Reda Jul 14 '15 at 18:20