Skip to content

Instantly share code, notes, and snippets.

View thjanssen's full-sized avatar
🎓
Creating the best Java persistence courses for the @Persistence-Hub

Thorben Janssen thjanssen

🎓
Creating the best Java persistence courses for the @Persistence-Hub
View GitHub Profile
@Entity
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
private Long id;
@Column
private String value;
List<Author> authors = em.createQuery(“SELECT a FROM Author a ORDER BY a.id”, Author.class)
.setMaxResults(5)
.setFirstResult(0)
.getResultList();
Book b = new Book();
b.setTitle(“The Hound of the Baskervilles”);
b.setPublishingDate(LocalDate.of(1902, 4, 30));
em.persist(b);
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
}
StoredProcedureQuery q = this.em.createNamedStoredProcedureQuery(“getReviews”);
q.setParameter(2, b.getId());
List<Review> reviews = q.getResultList();
@Entity
public class Author {
@Column
private LocalDate dateOfBirth;
public int getAge() {
return return Period.between(dateOfBirth, LocalDate.now()).getYears();
@Entity(name = “BlogPost”)
public class BlogPost extends Publication {
@Column
private String url;
}
Session session = em.unwrap(Session.class);
SessionFactory sessionFactory = em.getEntityManagerFactory().unwrap(SessionFactory.class);
@Entity
@Immutable
public class BookView {
}
Statistics stats = sessionFactory.getStatistics();
long queryCount = stats.getQueryExecutionCount();