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
em.createNamedQuery(“selectAuthors”).getResultList();
@Entity
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Column
private MyJson jsonProperty;
17:34:50,353 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_ where author0_.id=1
17:34:50,362 TRACE [org.hibernate.type.descriptor.sql.BasicExtractor] - extracted value ([id1_0_] : [BIGINT]) - [1]
17:34:50,373 TRACE [org.hibernate.type.descriptor.sql.BasicExtractor] - extracted value ([firstNam2_0_] : [VARCHAR]) - [Joshua]
17:34:50,373 TRACE [org.hibernate.type.descriptor.sql.BasicExtractor] - extracted value ([lastName3_0_] : [VARCHAR]) - [Bloch]
17:34:50,374 TRACE [org.hibernate.type.descriptor.sql.BasicExtractor] - extracted value ([version4_0_] : [INTEGER]) - [0]
@thjanssen
thjanssen / Book.java
Last active September 27, 2020 22:50
@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;
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
List<Object[]> results = em.createQuery("SELECT p.firstName, p.lastName, n.phoneNumber FROM Person p JOIN PhoneBookEntry n ON p.firstName = n.firstName AND p.lastName = n.lastName").getResultList();
for (Object[] result : results) {
log.info(result[0] + " " + result[1] + " - " + result[2]);
}
em.getTransaction().commit();
Object r = em.createQuery("SELECT function('calculate', a.id, 1) FROM Author a WHERE a.id = 1").getSingleResult();
@thjanssen
thjanssen / MyEntity.java
Created April 12, 2016 05:34
Persisting Java 8 DateTime API with Hibernate 5
@Entity
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Column
private LocalDate date;
@SqlResultSetMapping(
name = "AuthorValueMapping",
classes = @ConstructorResult(
targetClass = AuthorValue.class,
columns = {
@ColumnResult(name = "id", type = Long.class),
@ColumnResult(name = "firstname"),
@ColumnResult(name = "lastname"),
@ColumnResult(name = "numBooks", type = Long.class)}))
2015-12-09 04:51:31,426 DEBUG [org.hibernate.SQL] (default task-1) select nextval ('hibernate_sequence')
2015-12-09 04:51:31,456 DEBUG [org.hibernate.SQL] (default task-1) select nextval ('hibernate_sequence')
2015-12-09 04:51:31,591 DEBUG [org.hibernate.SQL] (default task-1) insert into Author (firstName, lastName, version, id) values (?, ?, ?, ?)
2015-12-09 04:51:31,595 DEBUG [org.hibernate.SQL] (default task-1) insert into Author (firstName, lastName, version, id) values (?, ?, ?, ?)
2015-12-09 04:51:31,599 DEBUG [org.hibernate.SQL] (default task-1) update Author set firstName=?, lastName=?, version=? where id=? and version=?
2015-12-09 04:51:31,603 DEBUG [org.hibernate.SQL] (default task-1) select author0_.id as id1_0_, author0_.firstName as firstNam2_0_, author0_.lastName as lastName3_0_, author0_.version as version4_0_ from Author author0_ where author0_.lastName=?
2015-12-09 04:51:31,615 INFO [org.hibernate.engine.internal.StatisticalLoggingSessionEventListener] (default task-1) Session Metrics {
// 1. create the Order
cart.start();
// 2. add one Item to the Order
cart.addItem("myFirstProduct");
// 3. add additional Item to the Order
cart.addItem("mySecondProduct");
// 4. write Order with its Items to the database
cart.persist();