Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@Entity(name =BlogPost”)
public class BlogPost extends Publication {
@Column
private String url;
}
@Entity(name =BlogPost”)
public class BlogPost extends Publication {
@Column
private String url;
}
@Entity(name =BlogPost”)
@DiscriminatorValue(“Blog”)
public class BlogPost extends Publication {
@Column
private String url;
}
@Entity(name =BlogPost”)
public class BlogPost extends Publication {
@Column
private String url;
}
@Entity(name =Book”)
public class Book extends Publication {
@Column
private int pages;
}
@Entity(name =Book”)
public class Book extends Publication {
@Column
private int pages;
}
@Entity(name =Book”)
@DiscriminatorValue(“Book”)
public class Book extends Publication {
@Column
private int pages;
}
@Entity(name =Book”)
public class Book extends Publication {
@Column
private int pages;
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Publication {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
protected Long id;
@Column
protected String title;
@Version
@Column(name = “version”)
private int version;
@ManyToMany
@JoinTable(name =PublicationAuthor”, joinColumns = { @JoinColumn(name = “publicationId”, referencedColumnName = “id”) }, inverseJoinColumns = { @JoinColumn(name = “authorId”, referencedColumnName = “id”) })
private Set authors = new HashSet();
@Column
@Temporal(TemporalType.DATE)
private Date publishingDate;
}
@MappedSuperclass
public abstract class Publication {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
protected Long id;
@Column
protected String title;
@Version
@Column(name = “version”)
private int version;
@Column
@Temporal(TemporalType.DATE)
private Date publishingDate;
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name =Publication_Type”)
public abstract class Publication {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
protected Long id;
@Column
protected String title;
@Version
@Column(name = “version”)
private int version;
@ManyToMany
@JoinTable(name =PublicationAuthor”, joinColumns = { @JoinColumn(name = “publicationId”, referencedColumnName = “id”) }, inverseJoinColumns = { @JoinColumn(name = “authorId”, referencedColumnName = “id”) })
private Set authors = new HashSet();
@Column
@Temporal(TemporalType.DATE)
private Date publishingDate;
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Publication {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
protected Long id;
@Column
protected String title;
@Version
@Column(name = “version”)
private int version;
@ManyToMany
@JoinTable(name =PublicationAuthor”, joinColumns = { @JoinColumn(name = “publicationId”, referencedColumnName = “id”) }, inverseJoinColumns = { @JoinColumn(name = “authorId”, referencedColumnName = “id”) })
private Set authors = new HashSet();
@Column
@Temporal(TemporalType.DATE)
private Date publishingDate;
}
List authors= em.createQuery(“SELECT a FROM Author a”, Author.class).getResultList();
for (Author a : authors) {
for (Publication p : a.getPublications()) {
if (p instanceof Book)
log(p.getTitle(), “book”);
else
log(p.getTitle(), “blog post”);
}
}
17:16:05,244 DEBUG [org.hibernate.SQL] – select author0_.id as id1_0_, author0_.firstName as firstNam2_0_, author0_.lastName as lastName3_0_, author0_.version as version4_0_ from Author author0_
17:16:05,280 DEBUG [org.hibernate.SQL] – select publicatio0_.authorId as authorId2_4_0_, publicatio0_.publicationId as publicat1_4_0_, publicatio1_.id as id1_3_1_, publicatio1_.publishingDate as publishi2_3_1_, publicatio1_.title as title3_3_1_, publicatio1_.version as version4_3_1_, publicatio1_1_.pages as pages1_2_1_, publicatio1_2_.url as url1_1_1_, case when publicatio1_1_.id is not null then 1 when publicatio1_2_.id is not null then 2 when publicatio1_.id is not null then 0 end as clazz_1_ from PublicationAuthor publicatio0_ inner join Publication publicatio1_ on publicatio0_.publicationId=publicatio1_.id left outer join Book publicatio1_1_ on publicatio1_.id=publicatio1_1_.id left outer join BlogPost publicatio1_2_ on publicatio1_.id=publicatio1_2_.id where publicatio0_.authorId=?
Effective Java is a book.
List authors= em.createQuery(“SELECT a FROM Author a”, Author.class).getResultList();
for (Author a : authors) {
for (Publication p : a.getPublications()) {
if (p instanceof Book)
log(p.getTitle(), “book”);
else
log(p.getTitle(), “blog post”);
}
}
16:04:32,073 DEBUG [org.hibernate.SQL] – select author0_.id as id1_0_, author0_.firstName as firstNam2_0_, author0_.lastName as lastName3_0_, author0_.version as version4_0_ from Author author0_
16:04:32,118 DEBUG [org.hibernate.SQL] – select publicatio0_.authorId as authorId2_2_0_, publicatio0_.publicationId as publicat1_2_0_, publicatio1_.id as id2_1_1_, publicatio1_.publishingDate as publishi3_1_1_, publicatio1_.title as title4_1_1_, publicatio1_.version as version5_1_1_, publicatio1_.pages as pages6_1_1_, publicatio1_.url as url7_1_1_, publicatio1_.Publication_Type as Publicat1_1_1_ from PublicationAuthor publicatio0_ inner join Publication publicatio1_ on publicatio0_.publicationId=publicatio1_.id where publicatio0_.authorId=?
Effective Java is a book.
List authors= em.createQuery(“SELECT a FROM Author a”, Author.class).getResultList();
for (Author a : authors) {
for (Publication p : a.getPublications()) {
if (p instanceof Book)
log(p.getTitle(), “book”);
else
log(p.getTitle(), “blog post”);
}
}
15:57:16,722 DEBUG [org.hibernate.SQL] – select author0_.id as id1_0_, author0_.firstName as firstNam2_0_, author0_.lastName as lastName3_0_, author0_.version as version4_0_ from Author author0_
15:57:16,765 DEBUG [org.hibernate.SQL] – select publicatio0_.authorId as authorId2_4_0_, publicatio0_.publicationId as publicat1_4_0_, publicatio1_.id as id1_3_1_, publicatio1_.publishingDate as publishi2_3_1_, publicatio1_.title as title3_3_1_, publicatio1_.version as version4_3_1_, publicatio1_.pages as pages1_2_1_, publicatio1_.url as url1_1_1_, publicatio1_.clazz_ as clazz_1_ from PublicationAuthor publicatio0_ inner join ( select id, publishingDate, title, version, null::int4 as pages, null::varchar as url, 0 as clazz_ from Publication union all select id, publishingDate, title, version, pages, null::varchar as url, 1 as clazz_ from Book union all select id, publishingDate, title, version, null::int4 as pages, url, 2 as clazz_ from BlogPost ) publicatio1_ on publicatio0_.publicationId=publicatio1_.id where publicatio0_.authorId=?
Effective Java is a book.
List books = em.createQuery(“SELECT b FROM Book b”, Book.class).getResultList();
17:14:20,844 DEBUG [org.hibernate.SQL] – select book0_.id as id1_3_, book0_1_.publishingDate as publishi2_3_, book0_1_.title as title3_3_, book0_1_.version as version4_3_, book0_.pages as pages1_2_ from Book book0_ inner join Publication book0_1_ on book0_.id=book0_1_.id
List books = em.createQuery(“SELECT b FROM Book b”, Book.class).getResultList();
15:38:36,020 DEBUG [org.hibernate.SQL] – select book0_.id as id1_2_, book0_.publishingDate as publishi2_2_, book0_.title as title3_2_, book0_.version as version4_2_, book0_.pages as pages5_2_ from Book book0_
List books = em.createQuery(“SELECT b FROM Book b”, Book.class).getResultList();
16:02:47,411 DEBUG [org.hibernate.SQL] – select book0_.id as id2_1_, book0_.publishingDate as publishi3_1_, book0_.title as title4_1_, book0_.version as version5_1_, book0_.pages as pages6_1_ from Publication book0_ where book0_.Publication_Type=’Book’
List books = em.createQuery(“SELECT b FROM Book b”, Book.class).getResultList();
15:56:21,463 DEBUG [org.hibernate.SQL] – select book0_.id as id1_3_, book0_.publishingDate as publishi2_3_, book0_.title as title3_3_, book0_.version as version4_3_, book0_.pages as pages1_2_ from Book book0_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.