Last active
September 20, 2021 14:43
-
-
Save dungdm93/62c36ca0bfab7749cbad to your computer and use it in GitHub Desktop.
[Java] [JPA] @mapkey example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
@Entity | |
public class Course { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
public int courseCode; | |
public String name; | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
@Entity | |
public class Semester { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
public int id; | |
public int year; | |
public int ordinal; | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
import java.util.Map; | |
@Entity | |
public class Student { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
public int studentId; | |
public String firstName; | |
public String lastName; | |
// (Required either @OneToMany --or--- @ManyToMany) | |
@ManyToMany(cascade = CascadeType.ALL) // if the map value is NOT a basic type. | |
@JoinTable(name = "ENROLLMENTS", | |
joinColumns = @JoinColumn(name = "STUDENT"), | |
inverseJoinColumns = @JoinColumn(name = "SEMESTER")) | |
@MapKeyJoinColumn(name = "COURSE") | |
public Map<Course, Semester> enrollment; | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@CollectionTable
@OneToMany
@OneToMany
@ManyToMany
@MapKeyColumn
@MapKeyTemporal
@MapKeyEnumerated
@MapKeyJoinColumn
@MapKeyColumn
if the map key is a basic type.If you don't specify the column name, the name of the property is
«field»_KEY
@MapKeyEnumerated
/[@MapKeyTemporal
](https://docs.oracle.com/javaee/7/api/javax/persistence/MapKeyTemporal.html @MapKeyTemporal) if the map key type is respectively an enum or aDate
.@MapKeyJoinColumn
/@MapKeyJoinColumns
if the map key type is another entity.@ElementCollection
is required if the map value is a basic type.Corresponding config annotation are
@CollectionTable
and@Column
.@OneToMany
or@ManyToMany
.Corresponding config annotation is
@JoinTable
.