Related Resources and Integration Testing
relationships between entities in Spring Data REST
the code snippets in this read are from Baeldung
- One-to-One Relationship => annotation
@OneToOne
Library Entity(Table/Relation)
@Entity
public class Library {
@Id
@GeneratedValue
private long id;
@Column
private String name;
@OneToOne
@JoinColumn(name = "address_id")
@RestResource(path = "libraryAddress", rel="address")
private Address address;
// standard constructor, getters, setters
}
Address Entity(Table/Relation)
@Entity
public class Address {
@Id
@GeneratedValue
private long id;
@Column(nullable = false)
private String location;
@OneToOne(mappedBy = "address")
private Library library;
// standard constructor, getters, setters
}
- One-to-Many Relationship => annotation
@OneToMany
Book Entity(Table/Relation)
@Entity
public class Book {
@Id
@GeneratedValue
private long id;
@Column(nullable=false)
private String title;
@ManyToOne
@JoinColumn(name="library_id")
private Library library;
// standard constructor, getter, setter
}
Library Entity(Table/Relation)
public class Library {
//...
@OneToMany(mappedBy = "library")
private List<Book> books;
//...
}
- Many-to-Many Relationship => annotation
@ManyToOne
Author Entity(Table/Relation)
@Entity
public class Author {
@Id
@GeneratedValue
private long id;
@Column(nullable = false)
private String name;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "book_author",
joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "author_id",
referencedColumnName = "id"))
private List<Book> books;
//standard constructors, getters, setters
}
Book Entity(Table/Relation)
public class Book {
//...
@ManyToMany(mappedBy = "books")
private List<Author> authors;
//...
}