[Java] - ORM on Java
Thursday, November 2, 2023
JPA
=========================================================================
Hibernate
Resources:
1. https://docs.jboss.org/hibernate/stable/orm/userguide/html_single/Hibernate_User_Guide.html#hql-select-clause
Problems:
1. Cannot POJO if we select specific fields in Query Statement. If used in RESTful API, The data response doesn't exist property field in JSON.
Data look like:
"grid": [["value1", "value2", "value3",....]]
Solution:
In HQL Statement, we use:
String queryStatement = "SELECT new <Entity Name> (property 1, property 2,... ) FROM <Entity Name>;
property 1, 2... must be same orders of Construct of Entity Name.
OR
String queryStatement = "SELECT new map (field 1 as field1, field2 as field2,... ) FROM <Entity Name>;
when query.getResultList() can POJO in your List.
2. Use LIKE statement in Hibernate SQL
Error SQL:
@NamedQuery(name = "SystemConfig.findManyByKey", query = "SELECT s FROM SystemConfig s WHERE s.key like ':key%'"),
Query query = null;
try {
query = currentSession.createNamedQuery("SystemConfig.findManyByKey", SystemConfig.class);
query.setParameter("key", key);
result = query.getResultList();
} catch (NoResultException noresult) {
log.warn("Not found config in system.", noresult);
closeTransaction();
return null;
}
Normal SQL:
@NamedQuery(name = "SystemConfig.findManyByKey", query = "SELECT s FROM SystemConfig s WHERE s.key like :key"),
Query query = null;
try {
query = currentSession.createNamedQuery("SystemConfig.findManyByKey", SystemConfig.class);
query.setParameter("key", key + '%');
result = query.getResultList();
} catch (NoResultException noresult) {
log.warn("Not found config in system.", noresult);
closeTransaction();
return null;
}
=========================================================================
MyBatis
=========================================================================
is updating...
Bài liên quan
Comments[ 0 ]
Post a Comment