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
StoredProcedureQuery query = this.em.createStoredProcedureQuery("get_reviews", Review.class);
query.registerStoredProcedureParameter(1, void.class, ParameterMode.REF_CURSOR);
query.registerStoredProcedureParameter(2, Long.class, ParameterMode.IN);
query.setParameter(2, b.getId());
List<Review> reviews = query.getResultList();
<persistence>
<persistence-unit name="my-persistence-unit">
...
<properties>
<property name="hibernate.generate_statistics" value="true" />
...
</properties>
</persistence-unit>
@Entity
public class Author implements Comparable<Author> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
private int version;
@Entity
@DynamicUpdate
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
@Entity
public class Author {
@ElementCollection
@MapKeyColumn(name = "address_type")
@MapKeyEnumerated(EnumType.STRING)
private Map<AddressType, Address>address = new HashMap<AddressType, Address>();
...
}
Order o = em.find(Order.class, 1L);
OrderItem i = new OrderItem();
i.setOrder(o);
o.getItems().add(i);
em.persist(i);
@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'")
@Entity
public class Author {
@Id
private Long id;
}
@Entity
public class Book {
@Min(100)
@Max(1000)
private int numPages;
}
TypedQuery<Book> q = em.createQuery("SELECT b FROM Book b "
+ "WHERE :double2 > function('calculate', b.price, :double1)"
, Book.class);