Skip to content

Instantly share code, notes, and snippets.

Avatar

Thorben Janssen thjanssen

View GitHub Profile
View CallAdHocStoredProcedureQuery.java
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();
View persistence.xml
<persistence>
<persistence-unit name="my-persistence-unit">
...
<properties>
<property name="hibernate.generate_statistics" value="true" />
...
</properties>
</persistence-unit>
View Author.java
@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;
View Book.java
@Entity
@DynamicUpdate
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
View AuthorAddressEmbeddable.java
@Entity
public class Author {
@ElementCollection
@MapKeyColumn(name = "address_type")
@MapKeyEnumerated(EnumType.STRING)
private Map<AddressType, Address>address = new HashMap<AddressType, Address>();
...
}
View AddBidirectionalOneToMany.java
Order o = em.find(Order.class, 1L);
OrderItem i = new OrderItem();
i.setOrder(o);
o.getItems().add(i);
em.persist(i);
View Author.java
@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'")
View BasicMapping.java
@Entity
public class Author {
@Id
private Long id;
}
View Book.java
@Entity
public class Book {
@Min(100)
@Max(1000)
private int numPages;
}
View CallCustomFunction.java
TypedQuery<Book> q = em.createQuery("SELECT b FROM Book b "
+ "WHERE :double2 > function('calculate', b.price, :double1)"
, Book.class);