Enhancing the model

Figure 903. Business and surrogate keys Slide presentation Create comment in forum
Business keys:
  • Application domain semantics.

  • End user visible values.

Surrogate keys:
  • Representing object identity.

  • Implementing references.

  • Opaque values, only equality comparison.

  • System generated.


Figure 904. Database generation strategies Slide presentation Create comment in forum
Mysql:
CREATE TABLE StudyCourse (
  id bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (id),... )
Oracle, DB2,...:
CREATE TABLE StudyCourse (
  id bigint(20) NOT NULL
  GENERATED ALWAYS AS IDENTITY,
  PRIMARY KEY (id),... )

Figure 905. INSERT omitting id Slide presentation Create comment in forum
INSERT
INTO StudyCourse (fullName, shortName)
VALUES('Mobile Media ', 'MMB');

Note

The id attribute is being omitted and to be generated by the database server.


Figure 906. Generated surrogate id Slide presentation Create comment in forum
@Entity public class StudyCourse {
  @Id 
  @GeneratedValue 
   (strategy=GenerationType.IDENTITY)
  Long id;
  @Column(... unique = true )
    private String shortName;

  @Column(...unique = true )
   private String fullName;
...
  @Override
  public String toString() {
    return fullName + "(" + shortName
     + ", id=" + id + ")";
  }}
CREATE TABLE StudyCourse (
  id bigint(20) NOT NULL
      AUTO_INCREMENT ,
  fullName varchar(150) NOT NULL,
  shortName varchar(3) NOT NULL,
  PRIMARY KEY (id), 
  UNIQUE KEY ... (fullName), 
  UNIQUE KEY ... (shortName) 
)

Defining the primary key.

Database auto generated value.

First business key.

Second business key.

Figure 907. StudyCourse creation Slide presentation Create comment in forum
final StudyCourse csm =
  new StudyCourse(
  "CSM", "Computer Science and Media");

log.info(csm);
tx.begin();
  entityManager.persist(csm);
tx.commit();
log.info(csm);
13:04:33,027 ...Computer Science ...
  (CSM, id=null)
...
  insert ...StudyCourse ...
13:04:33,068 ...Computer Science ...
   (CSM, id=1)

Figure 908. Entity Student Slide presentation Create comment in forum
@Entity public class Student {
  @Id @GeneratedValue(...) Long id;

  @Column(... unique = true)
  private String name;

  @ManyToOne
  StudyCourse studyCourse;

  public Student(final String name)
   {this.name = name;}
}
@Entity class StudyCourse {
...

  @OneToMany(
    mappedBy = "studyCourse")
  final List<Student> students
    = new Vector<>();

...
}

Figure 909. Database schema Slide presentation Create comment in forum
CREATE TABLE Student (
  id bigint(20) NOT NULL AUTO_INCREMENT,
  name varchar(30) NOT NULL,
  studyCourse_id bigint(20) DEFAULT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY (name),
  KEY ... (studyCourse_id),
  FOREIGN KEY (studyCourse_id) REFERENCES StudyCourse (id)
)

Figure 910. Providing enrollment Slide presentation Create comment in forum
@Entity public class Student {
...
  @ManyToOne
  StudyCourse studyCourse;

  public void enroll(
  final StudyCourse studyCourse) {
    studyCourse.add(this);
    this.studyCourse = studyCourse;
  }
...
}
@Entity
public class StudyCourse {

  @OneToMany(
    mappedBy = "studyCourse",
    cascade = CascadeType.ALL)
  final List<Student> students
    = new Vector<>();

  void add(final Student student){
    students.add(student);
  } ...}

Figure 911. CreateUniversity Slide presentation Create comment in forum
    final List<Student> students = new Vector<>();
    students.add(new Student("Jill Evans"));
    students.add(new Student("Peter Brown"));

    final StudyCourse csm = studyCourses.get(0);
    tx.begin();
      students.forEach(s -> s.enroll(csm));
    tx.commit();