Slowly I Turned (Setting up JPA for Java)

A lot of my java students have trouble getting JPA set up in Eclipse, so I have included here A way to get JPA installed and setup. I do not assert or even believe that it is the only way to set up JPA, but it is a way that works that my students can use 

1) First Create a New Java Project

2) Click the Next Button (not the Finish button) and get the Java Settings Screen.

3) Click on the Libraries tab so you can add the necessary jar files to the build path.

4) Click on the Add External JARs… button and select the eclipselink.jar file and click Ooen

5) Click on the Add External JARs… button and select javax.persistence_2.1.0.v201304241213.jar (or whatever the current version is)

6) Click on the Add External JARs… button and select ojdbc6.jar (or whatever the current version is) and click Finish.

7) Right-Click your project in the Project Explorer and select properties from the drop-down.

8) Select Project Facets from the List on the Left…

9) If you see the screen below, you need to click the “Convert to faceted form…” link…

10) This project has a Java version level that is too low. Make the window a little wider so that you can see the controls to increase the java level.

11) Click the drop-down triangle next to the number that is on the line with the Java Project Facet

12) Click on the checkbox next to JPA and you will be asked to do some configuration. Click the link and get the following screen. Set the platform to EclipseLink, the JPA Implementation Type to Disable Library Configuration, and the Connection to the name of your connection. Once this is done click the connect link immediately under and to the right of the connection drop down. And then click OK.

13) Click Apply to save your JPA settings…

14) Look in the JPA Content container inside your project and double-click it.

15) This is the dialog box for configuring your persistence.xml file

16) If you click the General tab, you will see the raw xml of the persistence file

17) On the General tab, set the Persistence Provider to   org.eclipse.persistence.jpa.PersistenceProvide

18) Uncheck the Exclude unlisted classes checkbox…

19) On the connection tab, change the Transcation Type to Resource Local...

20) After that, click the Populate from connection link…

21) A dialog will appear asking you to select your connection, click Ok after you have done so…

22) The dialog should now look like this…

23) If you click on the source tab, the XML should now look approximately like this… Save the file changes and you are done…

24) Right-Click your project and Select New… JPA Entities from Tables…

25) This is the Generate Custom Entities dialog. If you completed all the previous steps you should see your connections listed and when connected you will see your schemas and tables listed…

26) Once you have selected the Connection, the Schema and at least one Table, you should see the Next button light up...

27) This screen will be blank if you have only selected one table (and possibly even if you have selected multiple tables but they don’t have any relationship to each other

28) Hit Next on this screen too…

29) Hit Next again (but this is where you would customize the name of the class and/or the names of the columns)…

30) Once you click Finish, you should right-click the project name again…

31) Select New… Class…

32) This is the class creation dialog, the main things you want to do are set your Package to customTools, your name to DBUtil and then click Finish…

33) You will get a DBUtil.java class on your screen ready for editing. Edit it to look something like this…

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class DBUtil {
 private static final EntityManagerFactory emf =
Persistence.createEntityManagerFactory("name of your persistence unit");
 public static EntityManagerFactory getEmFactory() {
return emf;
 }
}

34) Right-Click the Project Name and Select New… Class…

35) For this class just pick a name and check the public static void main(String[] args) checkbox…

36) After you click Finish, edit the class to look like this:

import javax.persistence.EntityManager;
import data.DBUtil;

public class QS3 {
 public static void main(String[] args) {
EntityManager em = DBUtil.getEmFactory().createEntityManager();
try {
model.DemoCustomer cust = em.find(model.DemoCustomer.class, (long)2);
System.out.println(cust.getCustFirstName());
} catch (Exception e){
System.out.println(e);
} finally {
em.close();
System.out.println("cerrado!");
}
 }
}

 

37)  Edit the Generated Class and add , schema=”TESTUSER” to the table annotation like you see on line 13.

38) Now you are done…Run it and see where you are at…