Java Persistence/MySQL

From Wikibooks, open books for an open world
< Java Persistence
Jump to: navigation, search

Using MySQL with Java Persistence API is quite straightforward as the JDBC driver is available directly from MySQL web site [1].

MySQL Connector/J is the official JDBC driver for MySQL and has a good documentation available directly on the web site.

In this page we will see some aspects of managing MySQL using the Persistence API.

Installing[edit]

Installation is straightforward and consists in making the .jar file downloaded from MySQL web site visible to the JVM. It can be already installed if you are using Apache or JBOSS.

Configuration tips[edit]

You can learn a lot from the documentation on MySQL web site [2].

Creating the database automatically[edit]

If you intend to create table and a database automatically , you will need to have the correct user rights but also inform the Persistence API about the name of the Database you want to create. For example with TopLink, if you used:

 <property name="toplink.ddl-generation" value="create-tables"/>

in the property.xml, you will be likely need to create a new database. To create the database "NewDB" automatically, you need to give the following URL to the jdbc connection:

 <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/NewDB?createDatabaseIfNotExist=true"/>

If not, the persistence API will complain that the database does not exist.