Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Last active September 27, 2020 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thjanssen/55c5a9067991767eb6bfc92a63bf00ae to your computer and use it in GitHub Desktop.
Save thjanssen/55c5a9067991767eb6bfc92a63bf00ae 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;
@Version
@Column(name = "version")
private int version;
@Column
private String title;
@OneToMany(mappedBy = "book")
private Set<BookPublisher> publishers = new HashSet<BookPublisher>();
...
}
@Entity
public class BookPublisher {
@EmbeddedId
private BookPublisherId id;
@ManyToOne
@JoinColumn(name = "fk_book", insertable = false, updatable = false)
private Book book;
@ManyToOne
@JoinColumn(name = "fk_publisher", insertable = false, updatable = false)
private Publisher publisher;
@Column
@Enumerated(EnumType.STRING)
private Format format;
public BookPublisher(Book b, Publisher p, Format f) {
// create primary key
this.id = new BookPublisherId(b.getId(), p.getId());
// initialize attributes
this.book = b;
this.publisher = p;
this.format = f;
// update relationships to assure referential integrity
p.getBooks().add(this);
b.getPublishers().add(this);
}
...
}
@Entity
public class BookPublisher {
@Embeddable
public static class BookPublisherId implements Serializable {
@Column(name = "fk_book")
protected Long bookId;
@Column(name = "fk_publisher")
protected Long publisherId;
public BookPublisherId() {
}
public BookPublisherId(Long bookId, Long publisherId) {
this.bookId = bookId;
this.publisherId = publisherId;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((bookId == null) ? 0 : bookId.hashCode());
result = prime * result
+ ((publisherId == null) ? 0 : publisherId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BookPublisherId other = (BookPublisherId) obj;
if (bookId == null) {
if (other.bookId != null)
return false;
} else if (!bookId.equals(other.bookId))
return false;
if (publisherId == null) {
if (other.publisherId != null)
return false;
} else if (!publisherId.equals(other.publisherId))
return false;
return true;
}
}
...
}
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Book b = new Book();
b.setTitle("My Book");
em.persist(b);
Publisher p = new Publisher();
p.setName("My Publisher");
em.persist(p);
BookPublisher bp = new BookPublisher(b, p, Format.EBOOK);
em.persist(bp);
em.getTransaction().commit();
em.close();
@Entity
public class Publisher {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
@Column(name = "version")
private int version;
@Column
private String name;
@OneToMany(mappedBy = "publisher")
private Set<BookPublisher> books = new HashSet<BookPublisher>();
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment