- Entity and Value Types / A Simple Person Entity
- Creating Departments and Adding Person Entities / Named Queries
- Setting Up Your Own JPA Eclipse Project / Importing the Downloaded JPA Eclipse Project / Setting Up the MySQL Database
- Checking the Contents of the Database / Explaining the Relationship Between the Entities
- Setting Up the Persistence Unit / Running the Code / Conclusion
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:
- Download the latest copy of Hibernate.
- Download the MySQL JDBC connector JAR file.
- In Eclipse, select File > New > Other > JPA > JPA Project.
- Click Next.
- Type a project name, select No Runtime, select Minimal JPA 2 Configuration, and click Next.
- Click Next twice.
- In JPA Facet, click Add Connection and select MySQL, then click Next.
- In URL, change Database to Quickstart.
- Enter the password you set up at installation time.
- Click Test Connection, and if all is well, you should see a message saying “Ping succeeded.”
- Click Finish.
- 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:
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:
- Unzip the file.
- Open Eclipse.
- 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 7Creating 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.