In my daily work, JPA is used in a Java EE environment/container. Here is a simple example
using JPA in a Java SE environment, in which it’s more easily to demonstrate usage of JPA.
Source code of this example is available on github.
This example is managed by Maven, and tries to involve as little dependencies as possible.
It uses Hibernate as JPA vendor. And Mysql is taken as the database server. By passing some
options, this example also helps create corresponding schema in database.
pom.xml
hibernate-core is not enough to use Hibernate as JPA provider. hibernate-entitymanager
should be used instead. For JPA dependency, here it use artifact javax.persistence from
group org.eclipse.persistence, not artifact persistence-api from group javax.persistence,
which is too old to use.
And logback dependency is added for logging purpose.
Entities
There are four entities in this example, Customer, Product, OrderLine, and Order.
Only list code for OrderLine and Order below. For other, please check them on github.
Type and Converter for Date Time
It’s said Joda time should be used as THE date time class prior to Java 8. Since this example
is developed in Java 7, so I take this advice.
To Use Joda time in JPA, I add a converter for it.
With this converter, createdTime of Order can be of org.joda.time.DateTime type.
A Simple Repository Class to Manipulate Entites
Add an Entity to Database
Below is code snippet from MainClass.
Schema Generation from JPA Metadata
JPA and JPA providers provide APIs to help generate schema in database, drop schema,
export schema to script files, execute SQL scripts to prepare data, and etc.
Below snippet from MainClass is to create schema in database using standard JPA API (though it has
some hanging threads problem).
Below snippet is output schema, but not execute it as the above one. It’s using Hibernate API.
Others
At first, I tried to use pure standard JPA API to implement this example. However, it turned out
not possible as what I thought to be. For example, the standard Persistence.generateSchema() API
does not mention any hanging database connection thread problem. Here Hibernate as the provider decides
not to close the database connection thread in Java SE environment even after code is finished running
in main thead. To eliminate this problem, I have to use Hibernate API to fulfill my task. So whichever
JPA provider you pick, you have to bind with it to some extend.
2015-08-1113:24
如果你觉得这篇文章对你有用,可以微信扫一扫表示🙏 / If you find this post is useful to you, buy me 🍶 via Wechat
A Simple Example Using JPA
In my daily work, JPA is used in a Java EE environment/container. Here is a simple example using JPA in a Java SE environment, in which it’s more easily to demonstrate usage of JPA. Source code of this example is available on github.
This example is managed by Maven, and tries to involve as little dependencies as possible. It uses Hibernate as JPA vendor. And Mysql is taken as the database server. By passing some options, this example also helps create corresponding schema in database.
pom.xml
hibernate-core
is not enough to use Hibernate as JPA provider.hibernate-entitymanager
should be used instead. For JPA dependency, here it use artifactjavax.persistence
from grouporg.eclipse.persistence
, not artifactpersistence-api
from groupjavax.persistence
, which is too old to use.And logback dependency is added for logging purpose.
Entities
There are four entities in this example,
Customer
,Product
,OrderLine
, andOrder
. Only list code forOrderLine
andOrder
below. For other, please check them on github.Type and Converter for Date Time
It’s said Joda time should be used as THE date time class prior to Java 8. Since this example is developed in Java 7, so I take this advice.
To Use Joda time in JPA, I add a converter for it.
With this converter,
createdTime
ofOrder
can be oforg.joda.time.DateTime
type.A Simple Repository Class to Manipulate Entites
Add an Entity to Database
Below is code snippet from
MainClass
.Schema Generation from JPA Metadata
JPA and JPA providers provide APIs to help generate schema in database, drop schema, export schema to script files, execute SQL scripts to prepare data, and etc.
Below snippet from
MainClass
is to create schema in database using standard JPA API (though it has some hanging threads problem).Below snippet is output schema, but not execute it as the above one. It’s using Hibernate API.
Others
At first, I tried to use pure standard JPA API to implement this example. However, it turned out not possible as what I thought to be. For example, the standard
Persistence.generateSchema()
API does not mention any hanging database connection thread problem. Here Hibernate as the provider decides not to close the database connection thread in Java SE environment even after code is finished running in main thead. To eliminate this problem, I have to use Hibernate API to fulfill my task. So whichever JPA provider you pick, you have to bind with it to some extend.如果你觉得这篇文章对你有用,可以微信扫一扫表示🙏 / If you find this post is useful to you, buy me 🍶 via Wechat