Skip to content

Instantly share code, notes, and snippets.

@danthe1st
Last active April 8, 2023 21:36
Show Gist options
  • Save danthe1st/a7cab8cad5a3044b3562e4f83530c12c to your computer and use it in GitHub Desktop.
Save danthe1st/a7cab8cad5a3044b3562e4f83530c12c to your computer and use it in GitHub Desktop.
JPA data class checklist and example persistence.xml for EclipseLink
  • 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 Sets)
    Eclipse: Shift+Alt+S
    IntelliJ: Alt+Insert

Relations

Inheritance

  • annotate superclass with @Inheritance(strategy = InheritanceType.JOINED) (this would create one table per entity, you could also use TABLE_PER_CLASS (one table per superclass and subclasses) or SINGLE_TABLE (one table for the whole hierachy) instead of JOINED)

1:1

bidirectional

  • stronger class:
   @OneToOne
   @JoinColumn(name="<name of column>")
  • weaker class:
   @OneToOne(mappedBy="<attribute name on other side>")

unidirectional

    @OneToOne
    @JoinColumn(name = "<column name>")

1:n

bidirectional

  • 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>")

unidirectional

  • Class with * in UML diagram:
   @ManyToOne

OR

  • Class with 1 in UML diagram:
    @OneToMany
    List<>

n:m

bidirectional

  • 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>")

unidirectional

    @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>")}
    )

Hints

  • @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.
<!-- example persistence.xml for EclipseLink -->
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<!-- TODO replace DATABASENAME/SCHEMANAME, create that DB, assign permissions to DB, replace USERNAME/PASSWORD with credentials that can access this schema -->
<persistence-unit name="MSSQL" transaction-type="RESOURCE_LOCAL">
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!-- use jakarta instead of javax if you use jakarta EE -->
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:sqlserver://localhost:1433;databaseName=DATABASENAME" />
<property name="javax.persistence.jdbc.user" value="<USERNAME>" />
<property name="javax.persistence.jdbc.password" value="<Password>" />
<!--EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
<persistence-unit name="MYSQL" transaction-type="RESOURCE_LOCAL">
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!-- use jskarta instead of javax if you use jakarta EE -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/SCHEMANAME?serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="<USERNAME>" />
<property name="javax.persistence.jdbc.password" value="<PASSWORD>" />
<!--EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment