Ruben Gamboa
Professor
Movies(_title_, _year_, length, genre)
Stars(_name_, address)
Studios(_name_, address)
StarsIn(movieTitle, movieYear, starName)
Owns(movieTitle, movieYear, studioName)
Movies(_title_, _year_, length, genre, studioName)
Stars(_name_, address)
Studios(_name_, address)
StarsIn(title, year, name)
Of course, we can avoid adding a relationship table for the many-to-one relationship
Movies(_title_, _year_, length, genre)
MurderMysteries(_title_, _year_, length, genre, weapon)
Cartoons(_title_, _year_, length, genre)
CartoonMurderMysteries(_title_, _year_, length, genre, weapon)
Using the single-class-per-path approach
Movies(_title_, _year_, length, genre)
MurderMysteries(_title_, _year_, weapon)
Cartoons(_title_, _year_)
CartoonMurderMysteries(_title_, _year_, weapon)
Using the table-per-class approach
Movies(_title_, _year_, length, genre, type, weapon)
Using the just-one-class approach
Studios(_name_, address)
Movies(_title_, _year_, length, genre, studioName)
MovieExecs(_cert#_, name, address, networth)
Presidents(_cert#_, studioName)
The translation to a schema is just as before
Studios(_name_, address)
Crews(_number_, _studioName_, crewChief)
@Entity // Maps to table called "FACULTY"
public class Faculty
{
private Long fid;
// Marks the field that corresponds to a key
@Id @GeneratedValue
public Long getFid() { return fid; }
public void setFid(Long fid) { this.fid = fid; }
// Automatically maps to column called "fname"
private String fname;
public String getFname () { return fname; }
public void setFname (String fname) {
this.fname = fname;
}
// Automatically maps to column called "specialty"
private String specialty;
public String getSpecialty () { return specialty; }
public void setSpecialty (String specialty) {
this.specialty = specialty;
}
// Maps to FK on Student(advisor) => Faculty(fid)
private Set<Student> advisees = new HashSet<Student> ();
@OneToMany(mappedBy="advisor")
public Set<Student> getAdvisees() {
return advisees;
}
}
@Entity // Maps to table called "STUDENTS"
@Table(name="STUDENTS")
public class Student
{
private Long sid;
// Marks the field that corresponds to a key
@Id @GeneratedValue
public Long getSid() { return sid; }
public void setSid(Long sid) { this.sid = sid; }
// Automatically maps to column called "sname"
private String sname;
public String getSname () { return sname; }
public void setSname (String sname) {
this.sname = sname;
}
// Automatically maps to column called "gpa"
private Float gpa;
public Float getGpa () { return gpa; }
public void setGpa (Float gpa) {
this.gpa = gpa;
}
private Faculty advisor;
@ManyToOne
public Faculty getAdvisor () { return advisor; }
public void setAdvisor (Faculty advisor) {
this.advisor = advisor;
}
Faculty f = ...;
for (Student advisee: f.getAdvisees()) {
// Send reminder to advisee
}
Faculty f = ...;
Student s = ...;
s.setAdvisor (f);
session.save (s);
session.getTransaction().commit();
@Inheritance(strategy=InheritanceType.JOINED)
on the parent class only@PrimaryKeyJoinColumn(name="ID")
@Entity // Maps to table called "PERSON"
@Inheritance(strategy=InheritanceType.JOINED)
public class Person
{
private Long id;
// Marks the field that corresponds to a key
@Id @GeneratedValue
public Long getId() { return id; }
public void setId(Long fid) { this.id = id; }
// Automatically maps to column called "name"
private String name;
...
}
@Entity // Maps to table called "FACULTY"
@PrimaryKeyJoinColumn(name="ID")
public class Faculty extends Person
{
// Inherited methods:
// public Long getId();
// public void setId(Long id);
// public String getName();
// ...
// Automatically maps to column called "specialty"
private String specialty;
public String getSpecialty () { return specialty; }
...
}
@Entity // Maps to table called "STUDENT"
@PrimaryKeyJoinColumn(name="ID")
public class Student extends Person
{
// Inherited methods:
// public Long getId();
// public void setId(Long id);
// public String getName();
// ...
// Automatically maps to column called "gpa"
private Float gpa;
public Float getGpa () { return gpa; }
...
}
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
on the parent class only@AttributeOverrides
to specify column names on subtables (optional)@Entity // Maps to table called "PERSON"
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Person
{
private Long id;
// Marks the field that corresponds to a key
@Id @GeneratedValue
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
// Automatically maps to column called "name"
private String name;
...
}
@Entity // Maps to table called "FACULTY"
@AttributeOverrides({
@AttributeOverride(name="id", column=@Column(name="id")),
@AttributeOverride(name="name", column=@Column(name="name"))
})
public class Faculty extends Person
{
// Inherited methods:
// public Long getId();
// public void setId(Long id);
// public String getName();
// ...
// Automatically maps to column called "specialty"
private String specialty;
public String getSpecialty () { return specialty; }
...
}
@Entity // Maps to table called "STUDENT"
@AttributeOverrides({
@AttributeOverride(name="id", column=@Column(name="id")),
@AttributeOverride(name="name", column=@Column(name="name"))
})
public class Student extends Person
{
// Inherited methods:
// public Long getId();
// public void setId(Long id);
// public String getName();
// ...
// Automatically maps to column called "gpa"
private Float gpa;
public Float getGpa () { return gpa; }
...
}
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
on the parent class only@DiscriminatorColumn
to specify the column that holds the type of the row, on the parent class only@DiscriminatorValue
on each class to specify the value of the discriminating column for objects of that class@Entity // Maps to table called "PERSON"
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="person")
public class Person
{
private Long id;
// Marks the field that corresponds to a key
@Id @GeneratedValue
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
// Automatically maps to column called "name"
private String name;
...
}
@Entity // Maps to table called "FACULTY"
@DiscriminatorValue(value="faculty")
public class Faculty extends Person
{
// Inherited methods:
// public Long getId();
// public void setId(Long id);
// public String getName();
// ...
// Automatically maps to column called "specialty"
private String specialty;
public String getSpecialty () { return specialty; }
...
}
@Entity // Maps to table called "STUDENT"
@DiscriminatorValue(value="student")
public class Student extends Person
{
// Inherited methods:
// public Long getId();
// public void setId(Long id);
// public String getName();
// ...
// Automatically maps to column called "gpa"
private Float gpa;
public Float getGpa () { return gpa; }
...
}