hibernate Query
hibernate Query
HQL
https://www.tutorialspoint.com/hibernate/hibernate_query_language.htm
Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.
query
|
Update
String hql = "UPDATE Employee set salary = :salary " + |
Insert
通常使用save or persist
INSERT INTO EntityName *`properties_list`* *`select_statement`* |
Query query = session.createQuery("insert into Stock(stock_code, stock_name)" + |
Delete
String hql = "DELETE FROM Employee " +
"WHERE id = :employee_id";
Query query = session.createQuery(hql);
query.setParameter("employee_id", 10);
int result = query.executeUpdate();
System.out.println("Rows affected: " + result);
String hql = "DELETE FROM Employee " + |
Criteria
https://howtodoinjava.com/hibernate/hibernate-criteria-queries-tutorial/#unique_result
Criteria API, which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical conditions.
The Hibernate Session interface provides createCriteria() method, which can be used to create a Criteria object that returns instances of the persistence object's class when your application executes a criteria query.
|
Projections
// single field |
Result Transformer
AliasToBeanResultTransformer
uses the default constructor of the DTO class to instantiate a new object. In the next step, Hibernate uses reflection to call a setter method for each aliased value in the query.
ToListResultTransformer and AliasToEntityMapResultTransformer
The ToListResultTransformer maps the Object[] returned by your query with all its elements to a java.util.List
The AliasToEntityMapResultTransformer transforms the query result to a java.util.Map that contains all aliased values of the result set. The alias of each value is used as the key of the Map.
Query selectPerson = session.createQuery( |
Implementing your own ResultTransformer
Query query = session.createNativeQuery("select id as personId, first_name as firstName, last_name as lastName, city from Person p") |
使用自定义SQL(非hibernate generated), 可以使用
- named sql
- createSQL
Create SQL
// 指定返回类型 |
NamedQuery
providing a centralized, quick and easy way to read and find *Entity*‘s related queries.
NamedQuery
org.hibernate.annotations.NamedQuery annotation
every *@NamedQuery* annotation is attached to exactly one entity class or mapped superclass. But, since the scope of named queries is the entire persistence unit, we should select the query name carefully to avoid a collision. And we have achieved this by using the entity name as a prefix.
.hibernate.annotations.NamedQueries({ |
Configs
cacheable – whether the query (results) is cacheable or not
cacheMode – the cache mode used for this query; this can be one of GET, IGNORE, NORMAL, PUT, or REFRESH
cacheRegion – if the query results are cacheable, name the query cache region to use
comment – a comment added to the generated SQL query; targetted for DBAs
flushMode – the flush mode for this query, one of ALWAYS, AUTO, COMMIT, MANUAL, or PERSISTENCE_CONTEXT
NamedNativeQuery
Since this is a native query, we'll have to tell Hibernate what entity class to map the results to. Consequently, we've used the *resultClass* property for doing this.
Another way to map the results is to use the *resultSetMapping* p
@org.hibernate.annotations.NamedNativeQueries( |
Notes; We can use the @NamedNativeQuery annotation to define calls to stored procedures and functions as well: