Creating entities

We have already created JavaBeans for Course, Person, Student, and Teacher. We will now convert them to JPA entities using the @Entity annotation. Open Course.java and add the following annotations:

@ManagedBean (name="course") 
@RequestScoped 
@Entity 
public class Course implements Serializable 

The same bean can act as a managed bean for JSF and an entity for JPA. Note that if the name of the class is different from the table name in the database, you will need to specify a name attribute of the @Entity annotation. For example, if our Course table were called SchoolCourse, then the entity declaration would be as follows:

@Entity(name="SchoolCourse") 

To specify the primary key of the Entity, use the @Id annotation. In the Course table, id is the primary key and is autogenerated. To indicate autogeneration of the value, use the @GeneratedValue annotation. Use the @column annotation to indicate that the member variable corresponds to a column in the table. So, the annotations for id are as follows:

@Id 
@GeneratedValue(strategy=GenerationType.IDENTITY) 
@Column(name="id") 
private int id; 

You can specify validations for a column using Bean Validation framework annotations, as mentioned earlier. For example, the course name should not be null:

@NotNull 
@Column(name="name") 
private String name; 

Furthermore, the minimum value of credits should be 1:

@Min(1) 
@Column(name="credits") 
private int credits;

In the preceding examples, the @Column annotation is not required to specify the name of the column if the field name is the same as the column name.

If you are using JPA entities to create tables and want to exactly specify the type of columns, then you can use the columnDefinition attribute of the @Column annotation; for example, to specify a column of type varchar with length 20, you could use @Column(columnDefinition="VARCHAR(20)").


Refer to https://javaee.github.io/javaee-spec/javadocs/javax/persistence/Column.html to see all the attributes of the @Column annotation.

We will add more annotations to Course Entity as needed later. For now, let's turn our attention to the Person class. This class is the parent class of the Student and Teacher classes. However, in the database, there is no Person table and all the fields of Person and Student are in the Student table; and the same for the Teacher table. So, how do we model this in JPA? Well, JPA supports inheritance of entities and provides control over how they should be mapped to database tables. Open the Person class and add the following annotations:

@Entity 
@Inheritance(strategy=TABLE_PER_CLASS) 
public abstract class Person implements Serializable { ... 

We are not only identifying the Person class as Entity, but we are also indicating that it is used for inheritance (using @Inheritance). The inheritance strategy decides how tables are mapped to classes. There are three possible strategies:

  • SINGLE_TABLE: In this case, fields of parent and child classes would be mapped to the table of the parent class. If we use this strategy, then the fields of Person, Student, and Teacher will be mapped to the table for the Person entity.
  • TABLE_PER_CLASS: In this case, each concrete class (non-abstract class) is mapped to a table in the database. All fields of the parent class are also mapped to the table for the child class. For example, all fields of Person and Student will be mapped to columns in the Student table. Since Person is marked as abstract, no table will be mapped by the Person class. It exists only to provide inheritance support in the application.
  • JOINED: In this case, the parent and its children are mapped to separate tables. For example, Person will be mapped to the Person table, and Student and Teacher will be mapped to the corresponding tables in the database.

As per the schema that we created for the JDBC application, we have Student and Teacher tables with all the required columns and there is no Person table. Therefore, we have selected the TABLE_PER_CLASS strategy here.


See more information about entity inheritance in JPA at https://javaee.github.io/tutorial/persistence-intro003.html#BNBQN.

The fields id, firstName, and lastName in the Person table are shared by Student and Teacher. Therefore, we need to mark them as columns in the tables and set the primary key. So, add the following annotations to the fields in the Person class:

  @Id 
  @GeneratedValue(strategy=GenerationType.IDENTITY) 
  @Column(name="id") 
  private int id; 
 
  @Column(name = "first_name") 
  @NotNull 
  private String firstName; 
 
  @Column(name = "last_name") 
  private String lastName; 

Here, column names in the table do not match class fields. Therefore, we have to specify the name attribute in @Column annotations.

Let's now mark the Student class as Entity:

@Entity 
@ManagedBean (name="student") 
@RequestScoped 
public class Student extends Person implements Serializable

The Student class has a Date field called enrolledSince, which is of the java.util.Date type. However, JDBC and JPA use the java.sql.Date type. If you want JPA to automatically convert java.sql.Date to java.util.Date, then you need to mark the field with the @Temporal annotation:

@Temporal(DATE) 
@Column(name="enrolled_since") 
private Date enrolledSince; 

Open the Teacher class and add the @Entity annotation to it:

@Entity 
@ManagedBean (name="teacher") 
@RequestScoped 
public class Teacher extends Person implements Serializable 

Then, map the designation field in the class:

@NotNull 
@Column(name="designation") 
private String designation; 

We have now added annotations for all tables and their fields that do not participate in table relationships. We will now model the relationships between tables in our classes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.229.44