Convert Sql query into Realm Query - java

I am using Realm (version 0.87). I want to convert the sql query
Select * from Users where 'firstname' + " "+ 'lastname' like "Ranjana Dangol".
I tried it as
RealmQuery<UserEntityData> query = realm.where(UserEntityData.class).beginGroup();
String searchQuery = "Ranjana Dangol"
String[] queries = searchQuery.split("\\s+");
for(String username : queries){
query.equalTo(UserEntityData.FIRST_NAME, username, Case.INSENSITIVE)
.equalTo(UserEntityData.LAST_NAME, username, Case.INSENSITIVE);
}

Related

use jpa nativequery multiple columns in object list array

Use jpa nativequery multiple columns in object list array
List<Object []> queryList = new ArrayList<>();
String[] arr = {"val1", "val2"};
queryList.add(arr);
String sql = SELECT * FROM TABLE A WHERE (A.COL1, A.COL2) IN (:queryList)
Query query = entityManager.createNativeQuery(sql);
query.setParameter("queryList", queryList);
In postgresql like this
SELECT * FROM TABLE A WHERE (A.COL1, A.COL2) IN (('val1', 'val2'), ('val3', 'val4'));
Here is the Exception
Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: record = bytea
建議:No operator matches the given name and argument types. You might need to add explicit type
Is this possible?
I would try to restructure the query as follows:
SELECT * FROM TABLE A
WHERE (A.COL1 = 'val1' and A.COL2 = 'val2')
OR (A.COL1 = 'val3' and A.COL2 = 'val4')
This would allow the query to be constructed as follows:
List<String[]> queryList = new ArrayList<>();
String[] arr = {"val1", "val2"};
String[] arr = {"val3", "val4"};
queryList.add(arr);
String sql = "SELECT * FROM TABLE A "; //dont forget space at end
if (!queryList.isEmpty()){
sql = sql + "WHERE "; //dont forget space at end
for (String[] queryParam : queryList ){
sql = sql + " (A.COL1 = '"+ queryParam[0] + "' and A.COL2 = '" + queryParam[1] + "') OR "; //dont forget space at end and simple colons for param
}
//finally remove the last OR.
Integer indexLastOR = sql.lastIndexOf("OR");
sql = sql.substring(0, indexLastOR);
}
Query query = entityManager.createNativeQuery(sql);
This will also allow the query to be implemented without being native, which is advisable to maintain the JPA philosophy.

JDBC template giving error for Query using "like"

I am not getting how to use %?% in the query below. It's giving me error:
PreparedStatementCallback; uncategorized SQLException for SQL [select
* from names where name like '%?%']
Problem:
String sql = "select * from names where name like ?";
List<NamesModel> names = jdbcTemplate.query(sql, new Object[]{searchName}, new NamesRowMapper());
Your exception tells you that you can't bind into quoted value. Below won't work:
String sql = "select * from names where name like '%?%'";
List<NamesModel> names = jdbcTemplate.query(sql,
new Object[]{ searchName }, new NamesRowMapper());
The correct way is to add % around searchName parameter value:
String sql = "select * from names where name like ?";
List<NamesModel> names = jdbcTemplate.query(sql,
new Object[]{ "%" + searchName + "%"}, new NamesRowMapper());

Hibernate or HQL Select on the result of another SELECT

Is there a way using HQL on the following query?
SELECT userId, pwd, pwdDate FROM
(SELECT userId, AES_DECRYPT(pwd, 'key_str') as pwd, pwdDate
FROM UserHistory order by pwdDate desc limit 5 ) AS A
WHERE pwd = :pwd
The following worked.
SELECT *
FROM UserHistory order by pwdDate desc limit 5
The above sql can be the following in hibernate.
Criteria criteria = session.createCriteria(UserHistory.class);
criteria.addOrder(Order.desc("pwdDate"));
List<UserHistory> list = criteria.setMaxResults(5).list();
The following worked. The key is to createSQLQuery for Native SQL.
String SQL =
"SELECT A.* FROM \n" +
"(select * \n" +
" from user_history$ order by pwdDate desc limit 5 ) AS A \n" +
"where pwd = AES_ENCRYPT(:pwd, 'key_str') \n";
Query query = session.createSQLQuery(SQL);
query.setParameter("pwd", psw);
List<UserHistory> list = query.list();

sql query does not give results

I am facing some issue with the following query.
for (String string : projects) {
String sql = "SELECT eff.id,eff.taskNo,eff.projectId,sum(eff.hours),eff.employeeId FROM EffortCalculator eff where eff.projectId='"
+ string + "' and eff.dayDate >= '2014-12-15' and eff.dayDate <= '2014-12-16' GROUP BY eff.projectId";
System.out.println("sql" + sql);
SQLQuery query = session.createSQLQuery(sql);
System.out.println(query.list());
// query.addEntity(EffortCalculator.class);
list.addAll(query.list());
}
When I execute this query in MySQL DB it works fine. It actually contains two rows. But when I use this query in hibernate it gives empty list.

hibernate query not working in java spring

I have this code and its working fine
Registration person = (Registration) session.get(Registration.class, 8);
person.setConfirmed(true);
session.save(person);
but this is not working , says mapping error
String query = "FROM registration WHERE user_id = 8";
Query query2 = session.createQuery(query);
Registration person = (Registration) query2.uniqueResult();
person.setConfirmed(true);
session.save(person);
This is my registration class
#Column(name = "user_id")
public Integer getUserId() {
return userId;
}
Because you are using a non-native query language, you probably need something like
String query = "FROM Registration WHERE userId = 8";
#Pasha, the following code is a SQL query not a HQL query.
String query = "FROM registration WHERE user_id = 8";
Query query2 = session.createQuery(query);
If you must run a SQL query, use the following instead:
String query = "FROM registration WHERE user_id = 8";
Query query2 = session.createSQLQuery(query);
query2.executeUpdate();
To convert the SQL query to a HQL query, assuming the Registration class has userId field:
String query = "FROM registration WHERE userId = 8";
Query query2 = session.createQuery(query);
query2.executeUpdate();
For a complete example, see the following guide: http://krams915.blogspot.com/2011/03/spring-hibernate-one-to-many.html

Categories

Resources