Created
April 13, 2017 06:42
-
-
Save thjanssen/519c85079fe3a4a1ebce5726deb5f1e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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(); | |
… | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Book { | |
… | |
public void addReview(Review review) { | |
this.reviews.add(review); | |
review.setBook(this); | |
} | |
… | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b = em.find(Book.class, 1L); | |
List reviews = b.getReviews(); | |
Assert.assertEquals(b, reviews.get(0).getBook()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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