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
Related
I am trying to find out a way to concatenate strings to create a JPA native query. At present I have 3 queries just for having 1 or 2 different words. For example:
String queryAsc = select emp.id,role_id from employee left join roles
on roles.id = emp.role_id
where emp.id = :id
order by employee.created_date asc;
String queryDesc = select emp.id,role_id from employee left join roles
on roles.id = emp.role_id
where emp.id = :id
order by employee.created_date desc;
Just because of the word difference of "asc" vs "desc", I have to write 2 different repository methods with JPA query as:
#Query(value = queryAsc,
countQuery = countToMyEmployees, nativeQuery = true)
Page<Object[]> findMyEmployeesAsc(#Param("id") String id, Pageable pageable);
#Query(value = queryDesc,
countQuery = countToMyEmployees, nativeQuery = true)
Page<Object[]> findMyEmployeesDesc(#Param("id") String id, Pageable pageable);
Is there a way to concatenate two strings in JPA query to avoid this kind of repetition?
Try using Sort from the caller.
if (condition) {
obj.findMyEmployees("123",pageableObject , new Sort("employee.created_date "));
} else {
obj.findMyEmployees("234",pageableObject ,new Sort(Sort.Direction.DESC,"employee.created_date"));
}
And rewrite the query as given below :
String query= select emp.id,role_id from employee left join roles
on roles.id = emp.role_id
where emp.id = :id;
#Query(value = query,
countQuery = countToMyEmployees, nativeQuery = true)
Page<Object[]> findMyEmployees(#Param("id") String id, Pageable pageable, Sort sort);
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);
}
Retrieve company table
GetEntity.java
String table = "company";
String q = "select * from " +table;
Query query = em.createNativeQuery(q, Company.class);
List<Company> list = query.getResultList();
...
Retrieve staff table
GetEntity.java
String table = "staff";
String q = "select * from " +table;
Query query = em.createNativeQuery(q, Staff.class);
List<Staff> list = query.getResultList();
...
My questions is how do I control the ? from the following:
em.createNativeQuery(q, ?);
List<?> list = q.getResultList();
Any ideas or suggestion?
Another option is to pass the Class entityClass as an argument to your find method and then you can try and derive table name from entityClass by using reflection and use the entityClass as the type argument to your createNativeQuery method.
Hope this helps!
I have one table, User, and in that table, I have 3 columns:
id
name
email
I want to retrieve a name for a particular email by passing the email id; how would I write that hibernate query?
Using a criteria query:
Criteria criteria = getSession().createCriteria(User.class);
criteria = criteria.add(Restrictions.eq("email", email));
List<User> results = (List<User>) criteria.list();
Using hql:
String hql = "from User where email = :email";
Query query = getSession().createQuery(hql);
query.setParameter("email", email);
List<User> results = (List<User>) query.list();
I want to execute a query using hibernate where the requirment is like
select * from user where regionname=''
that is select all the users from user where region name is some data
How to write this in hibernate
The below code is giving result appropraitely
Criteria crit= HibernateUtil.getSession().createCriteria(User.class);
crit.add(Restrictions.eq("regionName", regionName));
Well as you alaready said you can either use the Criteria API or create a HQL query:
// Criteria
List<User> users = HibernateUtil.getSession().createCriteria(User.class);
crit.add(Restrictions.eq("regionName", regionName)).list();
// HQL
String query = "SELECT FROM User WHERE regionName = :region";
List<User> users = HibernateUtil.getSession().createQuery(query).setString("region", regionName).list();
String hql = "SELECT u FROM User u WHERE regionName=:regionName";
Query q = session.createQuery(hql);
q.setParameter("regionName", regionName);
List result = q.list();