Skip to content

Instantly share code, notes, and snippets.

@shelajev
Last active July 5, 2016 10:41
Show Gist options
  • Select an option

  • Save shelajev/d1446aad71ad688d4bbca299a6eaba7c to your computer and use it in GitHub Desktop.

Select an option

Save shelajev/d1446aad71ad688d4bbca299a6eaba7c to your computer and use it in GitHub Desktop.
@Entity
@NamedEntityGraph(name = "graph.AuthorBooks", attributeNodes = @NamedAttributeNode("books"))
public class Author implements Serializable {
EntityGraph graph = entityManager.getEntityGraph("graph.AuthorBooks");
List<Author> authorsWithBooks = entityManager
.createQuery("SELECT DISTINCT a FROM Author a", Author.class)
.setHint("javax.persistence.loadgraph", graph)
.getResultList();
List<Author> authors = entityManager.createQuery("SELECT a FROM Author a LEFT JOIN FETCH a.books").getResultList();
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaUpdate<Author> update = cb.createCriteriaUpdate(Author.class);
update.from(Author.class);
// specify the update statement
update.set(Author_.firstName, "change me");
// specify the criteria for the update -- Author id >= 3
update.where(cb.greaterThanOrEqualTo(a.get(Author_.id), 3L));
entityManager.createQuery(update).executeUpdate();
Query::setFirstResult();
Query::setMaxResults();
entityManager.createQuery("SELECT a.id FROM Author a");
entityManager.createQuery("SELECT a FROM Author a");
@NamedStoredProcedureQuery(
name = "procedureNameInJava",
procedureName = "PROCEDURE_NAME_IN_DATABASE",
parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, type = Double.class, name = "a"),
@StoredProcedureParameter(mode = ParameterMode.OUT, type = Double.class, name = "c")
}
)
StoredProcedureQuery query = entityManager.createNamedStoredProcedureQuery("procedureNameInJava");
query.setParameter("a", 1.0);
query.execute();
Double c = (Double) query.getOutputParameterValue("c");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment