tags:

views:

81

answers:

5

I'm just writing a small java application and I would like to be able to persist the data model in a database. So I was wondering if I could use JPA for this. I used JPA some time ago, but as far as I remembered it required an application server. So I'm wondering can I just JPA to persists my classes w/o using an application server.

+3  A: 

Yes, you can use JPA without an application server. Here's a tutorial which may help you: TopLink JPA: How to use JPA with Java SE

Boris Pavlović
That's exactly what I was looking for, thanks!
Nils
It does not seem to be very complete, what about the data source? I want to use a JDBC data source, how can I create it and ensure that it's available when the application runs?
Nils
@Nils Define the connection properties in `persistence.xml` - http://snipplr.com/view/4450/sample-persistencexml-that-uses-hibernate--mysql/
Petar Minchev
thx :) Btw I found this more extensive example, which explains everything: http://java.sun.com/developer/technicalArticles/J2SE/Desktop/persistenceapi/
Nils
+1  A: 

Yes, you can use JPA without any application server. Look at section 2.4 in this tutorial for Hibernate.

Petar Minchev
+2  A: 

JPA is, umm, "traditionally" associated with application servers because JPA is part of the JEE spec. However, that doesn't mean individual implementations of JPA can't work outside of an app server.

I've personally done this with Hibernate, which is perhaps the most popular JPA implementation. The Hibernate documentation gives you some tips about how to run Hibernate in a standalone application.

Carl Smotricz
A: 

You're probably better off using Hibernate standalone; it's a bit easier to manage without the extra JPA layer on top. There isn't that much difference anyway.

Robert Watkins
Or, for a small-enough model/application, drop Hibernate all together and just use JDBC.
Robert Watkins
A: 

No J2EE server present in this tutorial http://www.datanucleus.org/products/accessplatform/guides/jpa/tutorial.html

DataNucleus