05. Java EE 6 - JPA 2.0

Lab Exercises


For the sake of the simplicity of the lab, most exercises are provided in the form of "ready-to-open-and-run" NetBeans projects.  (Many of them are borrowed from "glassfish-samples" and "Java EE 6 tutorial".)  Please feel free to create them from scratch if you want to. 

It is strongly encouraged, leveraging what is provided in this lab, you do your own experimentation meaning creating/adding your own code as much as you can. 

If you have written some code that might help everyone else, please feel free to share them on this codecamp email alias or directly send them to the instructors.  Your name will be recognized in this lab if your sample code is chosen to be included.  For the tasks that need to be done, please see the "Things to be done" section above.)

Before you start this lab, you have to get sample applications from "glassfish-samples" and "Java EE tutorial" as described in LAB-4530: Java EE 6 - Examples.

Exercise 0: Build standalone JPA application step by step

더보기

Exercise 1: Mapping with @ElementCollection

더보기


 

(1.3) Collection of Embeddable

더보기



 


(1.5) Multi-level Embeddables

더보기


 


(1.7) Embeddable with its own relationship

더보기


 


Exercise 2: Mapping with @OrderColumn

더보기



 

(2.3) @OrderColumn with one-to-many Unidirectional relationship

더보기



 


(2.5) @OrderColumn with one-to-many Bidirectional relationship

더보기


 


Exercise 3: Map key

더보기



 


(3.3) @MapKeyClass

더보기


 


Exercise 4: JPQL Impovements

더보기



 


(4.3) CASE

더보기


Exercise 5: Criteria API (without using Metamodel)

더보기


 

(5.3) Criteria API with "orderBy" method

더보기


 



(5.5) Criteria API with relationship traversal

더보기


 

(5.7) Criteria API with inner join

더보기

                                                                                                                       


Exercise 6: Criteria API (using Metamodel)

더보기

                                                                                                                 




(6.3) Open, build, and run "jpa2.0_criteria_where_metamodel2_Root_getModel" sample application

더보기

      Exercise 7: Locking

      더보기

                                                                                                                             


      Exercise 8: Caching API

      더보기

      Exercise 9: Build and run "order" sample application (from "Java EE 6 tutorial")

      더보기


       

      Exercise 10: Build and run "roster" sample application (from "Java EE 6 tutorial")

      더보기


      Homework Exercise


      The homework is revised since the original homework indeed has a flaw.  The code snippets are shown as hints.  The structure of the homework project is quite similar to the jpa2.0_mapping_collection_embeddables_relation project.

      1. The homework is to build a standalone JPA application called my_jpa2.0 project as following. 
      • Exercise point: Mapping with @ElementCollection
        • School entity class have schoolname field, which is String type, and district field, which is District class.  See a snippet of the School class below.
      ...
      @Entity
      public class School implements Serializable {

          @Id
          @GeneratedValue
          @Column(name = "SCHOOL_ID")
          private int id;
          private String schoolname;

          private District district;

        • The District class is Embeddable type and has a set of Student's.  It also has districtname field, which is String type. See a snippet of District class below.
      ...
      @Embeddable
      public class District implements Serializable{

          private String districtname;
       
          @OneToMany(cascade=CascadeType.ALL, mappedBy="school")
          private Set<Student> students;
      ...

        • The Student class is an entity class and  has studentname field, which is String type, and grade field, which is double type.  See a snippet of Student class below.
      ...
      @Entity
      @Table(name = "STUDENT_TABLE")
      public class Student implements Serializable {

          @Id
          @Column(name = "STUDENT_ID")
          @GeneratedValue
          private int id;
          private String studentname;
          private double grade;

          // Bi-directional one-to-many relationship
          @ManyToOne(optional = true)
          private School school;
      ...
        • In the Main.java, as samples, add at least 3 schools.  Each district of corresponding school has at least 2 students.
        • Exercise point: Criteria  API
          • In the Main.java, perform both JPQL and Criteria API as following.  Metamodel class does not have to be used.
            • Get all schools who have students whose grade is 3.0 or above
        2. Send the following files to javaee6-homeworks@sun.com with Subject as homework_javaee6_jpa2.0.
        • Zip file of the the my_jpa2.0 NetBeans project.  (Someone else should be able to open and run it as a NetBeans project.)  You can use your favorite zip utility or you can use "jar" utility that comes with JDK as following.
          • cd <parent directory that contains my_jpa2.0 directory> (assuming you named your project as my_jpa2.0)
          • jar cvf my_jpa2.0.zip my_jpa2.0 (my_jpa2.0 should contain nbproject directory)
        Please reduce the size of the zip file by right clicking the project and then selecting Clean before zipping the file.
        • Captured output screen  - name it as homework_javaee6_jpa2.0.gif or homework_javaee6_jpa2.0.jpg (or homework_javaee6_jpa2.0.<whatever graphics format>)
          • Any screen capture that shows that your program is working is good enough. 



        저작자 표시

        Trackback 0 Comment 0