Jakarta Persistence Query Language: Difference between revisions

Content deleted Content added
Tags: Mobile edit Mobile web edit
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 31:
Example JPA Classes, getters and setters omitted for simplicity.
 
<sourcesyntaxhighlight lang="java">
@Entity
public class Author {
Line 67:
private List<Book> books;
}
</syntaxhighlight>
</source>
 
Then a simple query to retrieve the list of all authors, ordered alphabetically, would be:
 
<sourcesyntaxhighlight lang="sql">
SELECT a FROM Author a ORDER BY a.firstName, a.lastName
</syntaxhighlight>
</source>
 
To retrieve the list of authors that have ever been published by XYZ Press:
 
<sourcesyntaxhighlight lang="sql">
SELECT DISTINCT a FROM Author a INNER JOIN a.books b WHERE b.publisher.name = 'XYZ Press'
</syntaxhighlight>
</source>
 
JPQL supports named parameters, which begin with the colon (<code>:</code>). We could write a function returning a list of authors with the given last name as follows:
 
<sourcesyntaxhighlight lang="java">
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
Line 97:
return query.getResultList();
}
</syntaxhighlight>
</source>
 
==Hibernate Query Language==