Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Created April 27, 2017 19:45
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/c85806c9b2c354dbe5da88a0cb5e9286 to your computer and use it in GitHub Desktop.
Save thjanssen/c85806c9b2c354dbe5da88a0cb5e9286 to your computer and use it in GitHub Desktop.
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@ManyToMany(mappedBy = "authors")
@Where(clause = "format = 'EBOOK'")
private List<Book> ebooks = new ArrayList<Book>();
@ManyToMany(mappedBy = "authors")
@Where(clause = "format = 'PAPERBACK'")
private List<Book> printBooks = new ArrayList<Book>();
...
}
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Enumerated(EnumType.STRING)
private Format format;
@ManyToMany
@JoinTable(name = "book_author",
joinColumns = {@JoinColumn(name = "fk_book")},
inverseJoinColumns = {@JoinColumn(name = "fk_author")})
private List<Author> authors = new ArrayList<Author>();
...
}
public enum Format {
PAPERBACK, EBOOK;
}
14:02:09,070 DEBUG [org.hibernate.SQL] -
select
author0_.id as id1_0_0_,
author0_.firstName as firstNam2_0_0_,
author0_.lastName as lastName3_0_0_,
author0_.version as version4_0_0_
from
Author author0_
where
author0_.id=?
14:02:09,109 DEBUG [org.hibernate.SQL] -
select
ebooks0_.fk_author as fk_autho2_2_0_,
ebooks0_.fk_book as fk_book1_2_0_,
book1_.id as id1_1_1_,
book1_.format as format2_1_1_,
book1_.title as title3_1_1_,
book1_.version as version4_1_1_
from
book_author ebooks0_
inner join
Book book1_
on ebooks0_.fk_book=book1_.id
and (
book1_.format = 'EBOOK'
)
where
ebooks0_.fk_author=?
14:02:09,117 DEBUG [org.hibernate.SQL] -
select
printbooks0_.fk_author as fk_autho2_2_0_,
printbooks0_.fk_book as fk_book1_2_0_,
book1_.id as id1_1_1_,
book1_.format as format2_1_1_,
book1_.title as title3_1_1_,
book1_.version as version4_1_1_
from
book_author printbooks0_
inner join
Book book1_
on printbooks0_.fk_book=book1_.id
and (
book1_.format = 'PAPERBACK'
)
where
printbooks0_.fk_author=?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment