Vaadin and JPA

Hello All

I’m an old Delphi (Yes I know!) developer - and I finally got around to reading the ‘Book of Vaadin’ thru completely…
This documentation is superb - a model of clarity - and I’ve really enjoyed myself!

I am struggling with binding data to a Vaadin Form or Table component to a MySQL Database - and the prospect of using Hibernate makes my heart sink…

I do not find the Hibernate example at all clear - that’s down to me…

Now this is all down to my own limitations, that’s for sure - can anyone help with using JPA/Dali tooling on a Vaadin Project. I’ve tried adding JPA as a facet to a Vaadin Project- but it just does not work for me.

Sure - using the MySQL Sakila Database I can create an entity class from a table - I’ve used the simple Actors Table from Sakila.

but:

I cannot update the persistence.xml file - I have no idea why…
Form Component shows the fields - but they are empty…

Now all this stuff would be trivial in Delphi - but now I’m really struggling…

Any guidance will be appreciated - meanwhile I’ll keep trying!

Kindest Regards

Jim Fergusson

Hello All

I hope this does not come across as to much of a moan - but I’ve had a frustrating day…

None of the following is a criticism of Vaadin.
I also know all of the tools I am using are Open Source - so I have to work at it - but surely connecting a web server to a database with a Java Application should be standard stuff…why is it so much of a pain?

I can run a standard Vaadin ‘no binding’ application on Tomcat - looks great…

Now for data binding.

JPA does not appear to work with Apache Tomcat - could not find anything in Tomcat help - but found info on the web that it can be done, but not without following many confusing - to me - pages of configuration. so I looked up Apache OpenJPA - Ah! - So Apache Geronimo supports OpenJPA natively!

Install Geronimo - but the Eclipse Geronimo Plug-in does not work - reported in July’09 - but still broken… so ‘workaround’ is to install from a nightly ‘unstable’ build - so OK at least it installs and I can create a Server Instance in Eclipse.

I can run a standard ‘no binding’ app on Geronimo - but as soon as Persistence is added it fails

16 hours so far - and I can’t display a single row from the Author’s table (MySQL Sakila Database)

I almost despair…

My son uses Code Ignitor and PHP - and can do this stuff in minutes…

Regards

Jim Fergusson

What kind of error messages are you getting? Does JPA create the database schema for you (in other words, does it even connect to the database)?

Where do you have you persistence.xml file? It needs to be in the META-INF folder, but I’ve noticed that putting the xml file in the WebContent/META-INF/ folder doesn’t help, as it needs to be in the classpath (and adding it in the project properties didn’t help either). What I had to do was to create a separete “META-INF” -folder
under the src-folder
, that’s right, in the same place with your code. When I put the peristence.xml file in that folder, everything started to work for me.

Hello Kim

You have no idea how much I appreciate your help…

Yes - JPA creates the schema OK

persistence.xml appears to be in the right place at: src/META_INF/persistence.xml - but I need to check the contents!

Really great of you to help

Kindest Regards from an old Scot!

Jim

Hello

I really appreciate the help so far…
At the risk of boring everyone, when it’s almost certainly my fault…

Contents of my persistence.xml file are:

<?xml version="1.0" encoding="UTF-8"?> org.apache.openjpa.persistence.PersistenceProviderImpl com.example.vaading4.Actor


Anything odd here?

Error message returned from Geronimo / Jetty

There were errors initializing your configuration: <openjpa-1.2.1-r752877:753278 fatal user error> org.apache.openjpa.util.UserException: A connection could not be obtained for driver class “com.mysql.jdbc.Driver” and URL “jdbc:mysql://localhost:3308/sakila”. You may have specified an invalid URL.
at org.apache.openjpa.jdbc.schema.DataSourceFactory.newConnectException(DataSourceFactory.java:256)
at org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:125)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.createConnectionFactory(JDBCConfigurationImpl.java:784)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getConnectionFactory(JDBCConfigurationImpl.java:691)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.openjpa.lib.conf.ConfigurationImpl.instantiateAll(ConfigurationImpl.java:288)
at org.apache.openjpa.conf.OpenJPAConfigurationImpl.instantiateAll(OpenJPAConfigurationImpl.java:1432)
at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:646)
at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:183)
at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:142)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:192)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:145)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:56)
at com.example.vaading4.Vaading4Application.init(Vaading4Application.java:17)
at com.vaadin.Application.start(Application.java:545)
at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.startApplication(AbstractApplicationServlet.java:1008)
at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:418)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.apache.geronimo.jetty6.InternalJettyServletHolder.handle(InternalJettyServletHolder.java:65)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.apache.geronimo.jetty6.handler.TwistyWebAppContext.access$101(TwistyWebAppContext.java:40)
at org.apache.geronimo.jetty6.handler.TwistyWebAppContext$TwistyHandler.handle(TwistyWebAppContext.java:65)
at org.apache.geronimo.jetty6.handler.ThreadClassloaderHandler.handle(ThreadClassloaderHandler.java:46)
at org.apache.geronimo.jetty6.handler.InstanceContextHandler.handle(InstanceContextHandler.java:58)
at org.apache.geronimo.jetty6.handler.UserTransactionHandler.handle(UserTransactionHandler.java:48)
at org.apache.geronimo.jetty6.handler.ComponentContextHandler.handle(ComponentContextHandler.java:47)
at org.apache.geronimo.jetty6.handler.TwistyWebAppContext.handle(TwistyWebAppContext.java:59)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:828)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.apache.geronimo.pool.ThreadPool$1.run(ThreadPool.java:214)
at org.apache.geronimo.pool.ThreadPool$ContextClassLoaderRunnable.run(ThreadPool.java:344)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in classloader org.apache.geronimo.configs/openjpa/2.1.4/car
at org.apache.geronimo.kernel.config.MultiParentClassLoader.loadOptimizedClass(MultiParentClassLoader.java:438)
at org.apache.geronimo.kernel.config.MultiParentClassLoader.loadClass(MultiParentClassLoader.java:280)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:87)
… 48 more


The url for mysql is correct - see anything else wrong??

Any help will be much appreciated

Regards

Jim Fergusson

Seems like you don’t have the mysql driver jar in your classpath. For example, I have mysql-connector-java-5.1.7-bin.jar in WebContent/WEB-INF/lib/. You can download the driver at
http://dev.mysql.com/downloads/connector/j/5.1.html

Hello Kim

I’m really
chuffed!!
(Scottish expression indicting happiness!)

I was well aware that this newbie problem had nothing to do with Vaadin…so you deserve thanks for even looking at it.

Works like a dream now!

Thanks so much for the care you took to solve it - I had the MySQL jar just about everywhere else but the classpath!
The beer is on me if we ever meet!

Vaadin really is superb -
the framework and you developers

Kindest Regards

Jim

No problem. Happy I was able to be of assistance :slight_smile: