Using jdbcTemplate.query with parameters - java

I have 3 tables in Database Lecture--< LectureGroups >-- Groups.
And I want to get schedule for a certain group on a certain day. I try to do it in this way:
#Repository
public class Schedule {
private static final String GET_GROUP_DAY_SCHEDULE = "SELECT * FROM LECTURES " +
"INNER JOIN LECTUREGROUPS ON LECTURES.ID = LECTUREGROUPS.LECTUREID " +
"INNER JOIN GROUPS ON GROUPS.ID = LECTUREGROUPS.GROUPID " +
"WHERE GROUPID = :GROUPID AND DATE = :DATE";
#Autowired
private JdbcTemplate jdbcTemplate;
public List<Lecture> getGroupDayLectures(int groupId, LocalDateTime dateTime) {
MapSqlParameterSource parameters = new MapSqlParameterSource()
.addValue("groupid", groupId)
.addValue("date", dateTime);
return jdbcTemplate.query(GET_GROUP_DAY_SCHEDULE, new BeanPropertyRowMapper<>(Lecture.class), parameters);
}
}
But I get an exception in query raw
Caused by: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.springframework.jdbc.core.namedparam.MapSqlParameterSource. Use setObject() with an explicit Types value to specify the type to use.
How I can fix it?
I also used variant with
private static final String GET_GROUP_DAY_SCHEDULE = "SELECT * FROM LECTURES " +
"INNER JOIN LECTUREGROUPS ON LECTURES.ID = LECTUREGROUPS.LECTUREID " +
"INNER JOIN GROUPS ON GROUPS.ID = LECTUREGROUPS.GROUPID " +
"WHERE GROUPID = ? AND DATE = ?";
#Autowired
private JdbcTemplate jdbcTemplate;
public List<Lecture> getGroupDayLectures(int groupId, LocalDateTime dateTime) {
return jdbcTemplate.query(GET_GROUP_DAY_SCHEDULE, new Object[]{groupId, dateTime}, new BeanPropertyRowMapper<>(Lecture.class));
}
and it works but return only 1 Lecture in list (it must be 3)

There is a signature with parameters in the jdbcTemplate class:
public <T> List<T> query(String sql, RowMapper<T> rowMapper, #Nullable Object... args)
So it is very easy to use it in this way
private static final String GET_GROUP_DAY_SCHEDULE = "SELECT * FROM LECTURES " +
"INNER JOIN LECTUREGROUPS ON LECTURES.ID = LECTUREGROUPS.LECTUREID " +
"INNER JOIN GROUPS ON GROUPS.ID = LECTUREGROUPS.GROUPID " +
"WHERE GROUPID = ? AND DATE = ?";
#Autowired
private JdbcTemplate jdbcTemplate;
public List<Lecture> getGroupDayLectures(int groupId, LocalDate date) {
return jdbcTemplate.query(GET_GROUP_DAY_SCHEDULE, new BeanPropertyRowMapper<>(Lecture.class), groupId, date);
}

Related

Update Database with JDBC having some values which should be ignored

I am having some troubles using JDBC for updating a table-column. If I have a table e.g User(name,address,hobby,...) imagine about 15 fields. Then I get via frontend an Object from a form, where a user can type in all entries which should be changed. Now I need to save the changes in the database, but not all of the fields got changed, so my DAO has some null values. For example name and address should be changed, the other entries in the table shouldn't. Is there any smart way to put that into a JDBC PreparedStatement? Or do you know other solutions? I am trying to avoid a lot of value != null statements.
Thanks in advance!
(I am using spring as my backend, and angular in frontend)
Since you're using Spring, you can use the NamedParameterJdbcTemplate, but the real trick is the use of COALESCE to use a fall-back value when the value given is NULL:
#Autowired
private DataSource dataSource;
public void updateUser(int id, String name, String address, String hobby) {
NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
String sql = "UPDATE User" +
" SET Name = COALESCE(:name, Name)" +
", Address = COALESCE(:address, Address)" +
", Hobby = COALESCE(:hobby, Hobby)" +
" WHERE Id = :id";
MapSqlParameterSource paramMap = new MapSqlParameterSource();
paramMap.addValue("id" , id , Types.INTEGER);
paramMap.addValue("name" , name , Types.VARCHAR);
paramMap.addValue("address", address, Types.VARCHAR);
paramMap.addValue("hobby" , hobby , Types.VARCHAR);
if (jdbcTemplate.update(sql, paramMap) == 0)
throw new EmptyResultDataAccessException("User not found: " + id, 1);
}
Or, if you use a POJO with the user data:
public class User {
private int id;
private String name;
private String address;
private String hobby;
// Getters and setters here
}
public void updateUser(User user) {
NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
String sql = "UPDATE User" +
" SET Name = COALESCE(:name, Name)" +
", Address = COALESCE(:address, Address)" +
", Hobby = COALESCE(:hobby, Hobby)" +
" WHERE Id = :id";
BeanPropertySqlParameterSource paramMap = new BeanPropertySqlParameterSource(user);
if (jdbcTemplate.update(sql, paramMap) == 0)
throw new EmptyResultDataAccessException("User not found: " + id, 1);
}
The simple solution you can have is using SimpleJdbcInsert and adding usingGeneratedKeyColumns("ID").
#Autowired
private DataSource dataSource;
#Override
public ResponseEntity<Void> insertEntity(Entity obj) {
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate.getDataSource());
simpleJdbcInsert.withTableName(TABLE_APP_REPO).usingGeneratedKeyColumns("ID");
BeanPropertySqlParameterSource paramSource = new BeanPropertySqlParameterSource(obj);
try {
simpleJdbcInsert.execute(paramSource);
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok().build();
}

Use native query with Spring Projection Interface to select fields

I want to know how can I populate these classes:
interface BaseDTO {
Integer getId();
String getNome();
Boolean getAtivo();
RegionalDTO getRegional();
MunicipioDTO getMunicipio();
}
interface RegionalDTO {
Integer getId();
}
interface MunicipioDTO {
Integer getId();
}
And run this query
#Query(value = "SELECT " +
"base.ID_BASE AS id, base.NOME AS nome, base.ATIVO AS ativo, " +
"regional.ID_REGIONAL AS ID_REGIONAL, " +
"municipio.ID_MUNICIPIO AS ID_MUNICIPIO " +
"FROM TB_BASE base " +
"LEFT JOIN TB_REGIONAL regional on base.ID_REGIONAL = regional.ID_REGIONAL " +
"LEFT JOIN TB_MUNICIPIO municipio on base.ID_MUNICIPIO = municipio.ID_MUNICIPIO " +
"ORDER BY base.ID_BASE", nativeQuery = true)
List<BaseDTO> getAll();
So I want to select the fields in the query, because later I will put more fields in theses DTO.
I think that the simplest way would be to use simple interface with only ids for regional and municipio. After calling getAll map this interface to appropriate class structure (where ids turn into dtos).
List<RealDTO> result = repository.getAll().stream()
.map(i -> new RealDTO(i))
.collect(Collectors.toList());
First, I need to convert my DTOS, to class
#Value
public class BaseDTO {
private Integer id;
private String nome;
private MunicipioDTO municipio;
private RegionalDTO regional;
public BaseDTO(Integer id, String nome, Integer municipioId, Integer regionalId) {
this.id = id;
this.nome = nome;
this.municipio = new MunicipioDTO(municipioId);
this.regional = new RegionalDTO(regionalId);
}
}
#Value
public class MunicipioDTO {
private Integer id;
}
#Value
public class RegionalDTO {
private Integer id;
}
So in my repository I did it
#Query(value = "SELECT new com.apivgosmobile.dto.model.BaseDTO(base.id, base.nome, municipio.id, regional.id)" +
"FROM Base base " +
"LEFT JOIN base.municipio municipio " +
"LEFT JOIN base.regional regional " +
"WHERE base.ativo = 1 " +
"ORDER BY base.id ASC")
List<BaseDTO> getAll();

NamedParameterJdbcTemplate unit test

How to create unit test of this service class:
#Service
public class PhoenixService implements IPhoenixService {
private Logger log = LoggerFactory.getLogger(PhoenixService.class);
private static final String WHERE = " WHERE ";
private static final String FROM = " FROM ";
private static final String SELECT = "SELECT ";
#Autowired
#Qualifier("phoenixNamedParameterJdbcTemplate")
private NamedParameterJdbcTemplate phoenixNamedParameterJdbcTemplate;
#Override
public List<Account> getInternalAccount(String accountNo) throws Exception {
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("accountNo", accountNo);
String queryGetId = SELECT + ACCOUNT_IDENTIFIER + FROM
+ "SCHEMA" + "."
+ ACCOUNT + WHERE
+ ACCOUNT_NUMBER_TEXT + " = :accountNo";
List<Account> accounts = phoenixNamedParameterJdbcTemplate.query(queryGetId, parameters,
new RowMapper());
if (accounts.isEmpty()) {
//SomeThing
}
return accounts;
}
}
I try to search but none working.
Sorry I new to Java especially unit test
Additional info, my environment didnt allow connection to DB for unit test.
Thanks

Can i reference a non mapped table in JPA CriteriaBuilder?

I am trying to re write the following query using entity manager..
public abstract class HibernateEntitySelector<T> extends HibernateDAOSupport implements EntitySelector<T> {
#Autowired
public void init(SessionFactory factory) {
setSessionFactory(factory);
}
public String SELECT_IDS = " IN (SELECT RESULT_ID FROM QUERY_RESULTS)";
public List<T> getEntitiesByIds(){
DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
criteria.add(Restrictions.sqlRestriction(getPrimaryKeyField()+SELECT_IDS));
return (List<T>) this.getHibernateTemplate().findByCriteria(criteria);
}
Something like this..
public abstract class HibernateEntitySelector<T> implements EntitySelector<T> {
public String SELECT_IDS = " IN (SELECT RESULT_ID FROM QUERY_RESULTS)";
#PersistenceContext
protected EntityManager em;
public List<T> getEntitiesByIds(){
String s = "FROM " + getEntityClass().getSimpleName() + " ent WHERE ent."+getEntityId()+SELECT_IDS;
Query query = this.em.createNamedQuery(s);
return (List<T>)query.getResultList();
}
}
But this fails due to QUERY_RESULTS not being mapped. Is there a way to do this without using the createNativeQuery method and then having to map all the columns manually?
You were quite close, though you are using:
Query query = this.em.createNamedQuery(s);
Use the createNativeQuery instead and alter the query string alongside also:
String s = "SELECT ent.* FROM " + getEntityClass().getSimpleName() + " ent WHERE ent."+getEntityId() + SELECT_IDS;
Query query = this.em.createNativeQuery(s, getEntityClass());
Try it out.

Hibernate SQL Query result Mapping/Convert TO Object/Class/Bean

1 2: select (table.*)/(all column) is OK
String sql = "select t_student.* from t_student";
//String sql = "select t_student.id,t_student.name,... from t_student"; //select all column
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Student.class);//or query.addEntity("alias", Student.class);
//query.list();[Student#..., Student#..., Student#...]
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); //or other transformer
query.list(); //[{Student(or alias)=Student#...},{Student=Student#...}]
3: select some column(not all of), is Error
String sql = "select t_student.id,t_student.name.t_student.sex from t_student";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Student.class);
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
query.list(); //Exception:invalid column/no column
I want "3" to work ok, and let the result can be mapped to Student.class.
Like: Student[id=?, name=?, sex=?, (other field are null/default)]
I've no idea for this error, help me please!
You can go further and add
.setResultTransformer(Transformers.aliasToBean(YOUR_DTO.class));
and automatically map it to your custom dto object, see also Returning non-managed entities.
For example:
public List<MessageExtDto> getMessagesForProfile2(Long userProfileId) {
Query query = getSession().createSQLQuery(" "
+ " select a.*, b.* "
+ " from messageVO AS a "
+ " INNER JOIN ( SELECT max(id) AS id, count(*) AS count FROM messageVO GROUP BY messageConversation_id) as b ON a.id = b.id "
+ " where a.id > 0 "
+ " ")
.addScalar("id", new LongType())
.addScalar("message", new StringType())
......... your mappings
.setResultTransformer(Transformers.aliasToBean(MessageExtDto.class));
List<MessageExtDto> list = query.list();
return list;
}
I want "3" to work ok, and let the result can be mapped to Student.class
That's possible using
Query createNativeQuery(String sqlString, String resultSetMapping)
In the second argument you could tell the name of the result mapping. For example:
1) Let's consider a Student entity, the magic is going to be in the SqlResultSetMapping annotation:
import javax.persistence.Entity;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;
#Entity
#Table(name = "student")
#SqlResultSetMapping(name = "STUDENT_MAPPING", classes = {#ConstructorResult(
targetClass = Student.class, columns = {
#ColumnResult(name = "name"),
#ColumnResult(name = "address")
})})
public class Student implements Serializable {
private String name;
private String address;
/* Constructor for the result mapping; the key is the order of the args*/
public Student(String aName, String anAddress) {
this.name = aName;
this.address = anAddress;
}
// the rest of the entity
}
2) Now you can execute a query which results will be mapped by STUDENT_MAPPING logic:
String query = "SELECT s FROM student s";
String mapping = "STUDENT_MAPPING";
Query query = myEntityManager.createNativeQuery(query, mapping);
#SuppressWarnings("unchecked")
List<Student> students = query.getResultList();
for (Student s : students) {
s.getName(); // ...
}
Note: I think it's not possible to avoid the unchecked warning.
There is only two ways.
You can use 1st or 2nd snippet. According to Hibernate documentation you must prefer 2nd.
You can get just a list of object arrays, like this:
String sql = "select name, sex from t_student";
SQLQuery query = session.createSQLQuery(sql);
query.addScalar("name", StringType.INSTANCE);
query.addScalar("sex", StringType.INSTANCE);
query.list();
I had same problem on HQL Query. I solved the problem by changing the transformer.
The problem caused the code written to transform as Map. But it is not suitable for Alias Bean. You can see the error code at below. The code written to cast result as map and put new field to the map.
Class : org.hibernate.property.access.internal.PropertyAccessMapImpl.SetterImpl
m
Method: set
#Override
#SuppressWarnings("unchecked")
public void set(Object target, Object value, SessionFactoryImplementor factory) {
( (Map) target ).put( propertyName, value );
}
I solved the problem to duplicate the transformer and change the code.
You can see the code in the project.
Link : https://github.com/robeio/robe/blob/DW1.0-migration/robe-hibernate/src/main/java/io/robe/hibernate/criteria/impl/hql/AliasToBeanResultTransformer.java
Class:
import java.lang.reflect.Field;
import java.util.Map;
import io.robe.hibernate.criteria.api.query.SearchQuery;
import org.hibernate.HibernateException;
import org.hibernate.transform.AliasedTupleSubsetResultTransformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AliasToBeanResultTransformer extends AliasedTupleSubsetResultTransformer {
private static final Logger LOGGER = LoggerFactory.getLogger(AliasToBeanResultTransformer.class);
private final Class resultClass;
// Holds fields of Transform Class as Map. Key is name of field.
private Map<String, Field> fieldMap;
public AliasToBeanResultTransformer(Class resultClass) {
if ( resultClass == null ) {
throw new IllegalArgumentException( "resultClass cannot be null" );
}
fieldMap = SearchQuery.CacheFields.getCachedFields(resultClass);
this.resultClass = resultClass;
}
#Override
public boolean isTransformedValueATupleElement(String[] aliases, int tupleLength) {
return false;
}
#Override
public Object transformTuple(Object[] tuple, String[] aliases) {
Object result;
try {
result = resultClass.newInstance();
for ( int i = 0; i < aliases.length; i++ ) {
String name = aliases[i];
Field field = fieldMap.get(name);
if(field == null) {
LOGGER.error(name + " field not found in " + resultClass.getName() + " class ! ");
continue;
}
field.set(result, tuple[i]);
}
}
catch ( InstantiationException e ) {
throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
} catch ( IllegalAccessException e ) {
throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
}
return result;
}
}
After created new Transformer You can use like below.
query.setResultTransformer(new AliasToBeanResultTransformer(YOUR_DTO.class));
You can mapped it automatically:
Your Model Student.java
public class Student {
private String name;
private String address;
}
Repository
String sql = "Select * from student";
Query query = em.createNativeQuery(sql, Student.class);
List ls = query.getResultList();
so it will automatically mapped the result with the Student class

Categories