Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Created April 13, 2017 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thjanssen/519c85079fe3a4a1ebce5726deb5f1e4 to your computer and use it in GitHub Desktop.
Save thjanssen/519c85079fe3a4a1ebce5726deb5f1e4 to your computer and use it in GitHub Desktop.
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
private Long id;
@OneToMany(mappedBy = “book”)
private List reviews = new ArrayList();
}
@Entity
public class Book {
public void addReview(Review review) {
this.reviews.add(review);
review.setBook(this);
}
}
Book b = em.find(Book.class, 1L);
Review r = new Review();
r.setComment(“This is a comment”);
r.setBook(b);
b.getReviews().add(r);
em.persist(r);
b = em.find(Book.class, 1L);
List reviews = b.getReviews();
Assert.assertEquals(b, reviews.get(0).getBook());
@Entity
public class Review {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
private Long id;
@ManyToOne
@JoinColumn(name = “fk_book”)
private Book book;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment