- Primary Key:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long oid;
- annotate all data classes with
@Entity
- Create attributes
- Annotate relations
- Create getters/Setters, equals/hashCode (required for
Set
s)
Eclipse:Shift
+Alt
+S
IntelliJ:Alt
+Insert
- annotate superclass with
@Inheritance(strategy = InheritanceType.JOINED)
(this would create one table per entity, you could also useTABLE_PER_CLASS
(one table per superclass and subclasses) orSINGLE_TABLE
(one table for the whole hierachy) instead ofJOINED
)
- stronger class:
@OneToOne
@JoinColumn(name="<name of column>")
- weaker class:
@OneToOne(mappedBy="<attribute name on other side>")
@OneToOne
@JoinColumn(name = "<column name>")
- Class with
1
in UML diagram (entity that exists only once per relation):
@OneToMany(mappedBy = "<attribute on other side>")
List<>
- Class with
*
in UML diagram (multiple instances of this entity exist per relation
@ManyToOne
@JoinColumn(name = "<name of column>")
- Class with
*
in UML diagram:
@ManyToOne
OR
- Class with
1
in UML diagram:
@OneToMany
List<>
- stronger class:
@ManyToMany(cascade = CascadeType.PERSIST)
@JoinTable(
name = "<name of table>",
joinColumns = {@JoinColumn (name = "<column name of attribute>")},
inverseJoinColumns = {@JoinColumn(name = "<column name of attribute on other side>")}
)
- weaker class:
@ManyToMany(mappedBy = "<name of attribute on other side>")
@ManyToMany (cascade = CascadeType.PERSIST)
@JoinTable(
name = "<name of table>",
joinColumns = {@JoinColumn (name = "<column name of attribute>")},
inverseJoinColumns = {@JoinColumn(name = "<column name of attribute on other side>")}
)
@JoinTable
and@JoinColumn
are optional. These annotations allow specific configuration how the database should be structured.@Table
can be added to entities in order to specify table names and unique constrains.