Home > Articles > Data > MySQL

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Setting Up Your Own JPA Eclipse Project

I always find it instructive to see how to create a working Eclipse project. So, here are the steps required to build the project from scratch. The full project is included in the article as a download, but it's not too hard to build it yourself. Just follow these steps:

  1. Download the latest copy of Hibernate.
  2. Download the MySQL JDBC connector JAR file.
  3. In Eclipse, select File > New > Other > JPA > JPA Project.
  4. Click Next.
  5. Type a project name, select No Runtime, select Minimal JPA 2 Configuration, and click Next.
  6. Click Next twice.
  7. In JPA Facet, click Add Connection and select MySQL, then click Next.
  8. In URL, change Database to Quickstart.
  9. Enter the password you set up at installation time.
  10. Click Test Connection, and if all is well, you should see a message saying “Ping succeeded.”
  11. Click Finish.
  12. Create a folder called META-INF, and copy the file persistence.xml into it.

You're done! The project is now set up. Next, add a lib folder and paste into it the required JARs from the Hibernate and MySQL downloads, namely:

  • lib/hibernate-commons-annotations-4.0.1.Final.jar
  • lib/hibernate-core-4.0.1.Final.jar
  • lib/hibernate-jpa-2.0-api-1.0.1.Final.jar
  • lib/mysql-connector-java-5.1.13-bin.jar

Remember to add the JAR files to the Eclipse project classpath. Next, in the src folder, create two folders: one called client and the other called model. Copy the two Java Client files into the client folder and copy the Department.Java and Person.java files into the model folder.

You now should be able to run the client program.

Importing the Downloaded JPA Eclipse Project

If you prefer to use the project from the download area, then you can import the download by following these steps:

  1. Unzip the file.
  2. Open Eclipse.
  3. Select File > Import > General > Existing Projects into Workspace.

You can then browse the code, execute the client, view the database, etc.

Setting Up the MySQL Database

Creating your own MySQL database is also pretty straightforward: Just download a copy of MySQL for your platform and run the setup program. Once MySQL is installed, just paste the contents of the following script after running the MySQL command prompt (see Listing 7):

Listing 7—Creating the Database

mysql -u root -p
ENTER YOUR PASSWORD HERE
mysql>  PASTE THE SCRIPT CONTENTS HERE
drop database quickstart;
create database quickstart;
use quickstart;
flush privileges;
CREATE TABLE `quickstart`.`DEPARTMENT_PERSON` (
  `Department_DEPT_ID` int unsigned not null auto_increment,
  `persons_PERSON_ID` int not null,
   PRIMARY KEY(`Department_DEPT_ID`, `persons_PERSON_ID`)       
)
ENGINE = InnoDB;
CREATE TABLE `quickstart`.`PERSON` (
  `PERSON_ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `firstName` VARCHAR(45) NOT NULL,
  `lastName` VARCHAR(45) NOT NULL,
  `friends` VARCHAR(45) NOT NULL,
  PRIMARY KEY(`PERSON_ID`)
)
ENGINE = InnoDB;
CREATE TABLE `quickstart`.`DEPARTMENT` (
  `DEPT_ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  PRIMARY KEY(`DEPT_ID`),
  UNIQUE(`name`)
)
ENGINE = InnoDB;

In Listing 7, I create three database tables: DEPARTMENT_PERSON, PERSON, and DEPARTMENT, respectively.

  • + Share This
  • 🔖 Save To Your Account