JPA Container + Hibernate

Hi,

I’m a bit confused about how the JPA Container and Hibernate can work together. I have annotated hibernate data objects. What should I do to connect the JPA container now to work with the hibernate’s configuration? Is there any tutorial or an example of configuration - how should I configure the persistence so JPA container uses hibernate?

Thanks in advance.

There is a rough description in the
Tutorial
, but this seems to be for Vaadin 6.

Another
article
points to example source for Vaadin 7.

For me none worked, I downloaded the
addressbook sample source
manually from the repository, added
LazyHibernateEntityManagerProvider
and
LazyHibernateServletFilter
to my project, and used these (to be used as servlet filter) classes in
AddressBookMainView
as told in the article above, for every built JPAContainer:


		persons = JPAContainerFactory.make(Person.class, JpaAddressbookUI.PERSISTENCE_UNIT);
		persons.getEntityProvider().setEntityManager(null);
		persons.getEntityProvider().setEntityManagerProvider(new LazyHibernateEntityManagerProvider());
		persons.getEntityProvider().setLazyLoadingDelegate(new HibernateLazyLoadingDelegate());

but ended with a lazy-loading exception:


org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.vaadin.demo.jpaaddressbook.domain.Department.persons, could not initialize proxy - no Session
	at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
	at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
	at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:137)
	at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:156)
	at com.vaadin.demo.jpaaddressbook.domain.Department.isSuperDepartment(Department.java:80)
	at com.vaadin.demo.jpaaddressbook.HierarchicalDepartmentContainer.areChildrenAllowed(HierarchicalDepartmentContainer.java:38)
	at com.vaadin.ui.Tree.areChildrenAllowed(Tree.java:740)
	at com.vaadin.ui.Tree.paintContent(Tree.java:610)
	at com.vaadin.server.LegacyPaint.paint(LegacyPaint.java:65)
	at com.vaadin.server.AbstractCommunicationManager.legacyPaint(AbstractCommunicationManager.java:1323)
	at com.vaadin.server.AbstractCommunicationManager.writeUidlResponse(AbstractCommunicationManager.java:810)
	at com.vaadin.server.AbstractCommunicationManager.getInitialUIDL(AbstractCommunicationManager.java:2528)
	at com.vaadin.server.AbstractCommunicationManager.handleBrowserDetailsRequest(AbstractCommunicationManager.java:2366)
	at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:325)
	at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:201)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
	at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)

Here are my Maven dependencies in pom.xml:


	<dependencies>
		<dependency>
			<groupId>com.vaadin</groupId>
			<artifactId>vaadin-server</artifactId>
			<version>7.0.3</version>
		</dependency>
		<dependency>
			<groupId>com.vaadin</groupId>
			<artifactId>vaadin-client-compiled</artifactId>
			<version>7.0.3</version>
		</dependency>
		<dependency>
			<groupId>com.vaadin</groupId>
			<artifactId>vaadin-client</artifactId>
			<version>7.0.3</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.vaadin</groupId>
			<artifactId>vaadin-themes</artifactId>
			<version>7.0.3</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.4</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>com.vaadin.addon</groupId>
			<artifactId>jpacontainer</artifactId>
			<version>3.0.0</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>4.2.0.Final</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>4.2.0.Final</version>
		</dependency>

		<dependency>
			<groupId>org.hsqldb</groupId>
			<artifactId>hsqldb</artifactId>
			<version>2.2.4</version>
		</dependency>

	</dependencies>

Here is my servlet definition in web.xml
(by the way, when using another url-pattern than /* in web.xml, I get an error-alert “Failed to load the bootstrap javascript ./…/VAADIN/vaadinBootstrap.js” in my web browser (=Chrome)):


    <filter>
        <filter-name>LazyHibernateServletFilter</filter-name>
        <filter-class>com.vaadin.addon.jpacontainer.itest.lazyhibernate.LazyHibernateServletFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>LazyHibernateServletFilter</filter-name>
        <servlet-name>JPA Container Demo Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
     
    <servlet>
        <servlet-name>JPA Container Demo Application</servlet-name>
        <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
            <init-param>
                <param-name>UI</param-name>
                <param-value>com.vaadin.demo.jpaaddressbook.JpaAddressbookUI</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>JPA Container Demo Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

[i]

[/i]

Hello, any updates on that one ? I have basically the same question, difficult to find an accurate example for Vaddin 7

I seem to have the exact same problem. I have this project that currently uses lazyquerycontainer 1.3.0 and it works fine. Then there was a need for TreeTable with lazy loaded objects through Container.Hierarchical. I tried JPAContainer, but only got the “could not initialize proxy - no Session”.

Here is an example project that illustrates the problem: http://k2c42.dy.fi/tmp/hibtestailu.tar.xz
You need postgresql that has “hibtestailu” database, user and password, then just “mvn install jetty:run” and go to localhost:8881/hibtestailu. Click “JPAContainer” button twice and get the org.hibernate.LazyInitializationException. The lazyquerycontainer button does the same, but with vaadin-lazyquerycontainer addon and doesn’t have any problems.

entityManager.contains(jpaContainer.getItem(…).getEntity()) returns false and
entityManager.contains(entityContainer.getEntity((Integer)itemId) returns true.

So with JPAContainer I will have to do entityManager.find or something to get a connected version of the object before using any lazy loaded fields. With EntityContainer of lazyquerycontainer addon the returned obejcts are already connected and I can just use them.

Is there a way to make JPAContainer return and use connected objects?