Skip to content

Instantly share code, notes, and snippets.

@MrSnyder
Last active August 7, 2020 14:16
Show Gist options
  • Save MrSnyder/50371bd2b4ebaee5b6de1ce19dd4e773 to your computer and use it in GitHub Desktop.
Save MrSnyder/50371bd2b4ebaee5b6de1ce19dd4e773 to your computer and use it in GitHub Desktop.
JPA Cheatsheet

JPA Cheatsheet

Minimal entity class mapping

Steps

  • Required: Annotate class with @Entity
  • Required: Define field with @Id annotation
  • NOTE: Prefer Lombok for automatic Getter/Setter generation.
@Entity
@Setter
@Getter
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
}

One-to-many mapping

Steps

  • Identify many side and annotate entity field (see below)
  • Annotate field (Set/List) on opposing entity side (see below)
@Entity
public class Book {
    @ManyToOne
    private Publisher publisher;
}

@Entity
public class Publisher {
    // HINT: refers to Book#getPublisher
    @OneToMany(mappedBy="publisher")
    private Set<Book> books;
}

Many-to-many mapping (without attributes)

Steps

  • Identify owning side and annotate entity field (see below)
  • Annotate field on opposing entity side (see below)
@Entity
public class Author {
    // owning side
    @ManyToMany
    private Set<Book> books;
}

@Entity
public class Book {
    // inverse side
    @ManyToMany(mappedBy = "books")
    private Set<Author> authors;
}

Caveats

Set or List?

It's usually preferrable to use Set:

  • If one uses List (on one or two sides of the asscociation) in the examples above, the join table will not have a primary key (using two Sets it will be a composite PK made up of the two foreign keys). This makes the relations to the join table identifiying (a pair of FKs identifies a row) and prevents the insertion of multiple rows with the same values.
  • Additionally, it has performance benefits when deleting associations between the two entities: https://vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate/

Many-to-many mapping (with attributes)

Steps

  • Create entity for JoinTable
  • Annotate fields in JoinTable entity (see below)
  • Annotate field opposing side 1 (see below)
  • Annotate field opposing side 2 (see below)
@Entity
@Table(name = "customer_order")
public class Order {

	@ManyToOne
	private Book book;
	
	@ManyToOne
	private Customer customer;

	private Integer amount;

}

@Entity
public class Book {
    @OneToMany(mappedBy = "book")
    private Set<CustomerOrder> orders;
}

@Entity
public class Customer {
    @OneToMany(mappedBy = customer")
    private Set<CustomerOrder> orders;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment