Select table row using HQL with param - java

I want to use this HQL query in order to select table row value:
String hql = "select e.token, e.name from Terminals e where e.token = ?";
Terminals terminal = entityManager
.createQuery(hql, Terminals.class)
.setParameter(0, terminalToken)
.getSingleResult();
But I get this result:
java.lang.IllegalArgumentException: Cannot create TypedQuery for query with more than one return using requested result type [org.api.entity.Terminals]
What is the proper way to implement this?

Your query return two Objects token and name and not a Terminals Object.
Instead you can use :
Object[] obj = entityManager
.createQuery(hql)
.setParameter(0, terminalToken)
.getSingleResult();
if(obj != null){
Terminals terminal = new Terminals((String) obj[0], (String) obj[1]);
}
Or you can create a constructor in Terminals class which hold two fields :
public Terminals(String token, String name){
this.token = token;
this.name = name;
}
then change your code to be :
String hql = "select new com.package.Terminals(e.token, e.name) from Terminals e where e.token = ?";
Terminals terminal = entityManager
.createQuery(hql, Terminals.class)
.setParameter(0, terminalToken)
.getSingleResult();

IMO, you should not directly call createQuery to assign to a custom object. And there is a possiblity of returning a list.
Query query = entityManager.createQuery(hql).setParameter(0, "something");<br>
List<Object[]> terminals = query.getResultList();
and retrieve the objects with an index as the ff as an example:
StudentDto dto = new StudentDto();
for(Object[] a: std) {
dto = new StudentDto();
dto.setId((long) a[0]); //id
dto.setName(a[1].toString()); //first name
dto.setPassportNumber(a[2].toString()); //last name
}

Related

How to Set parameter to null value in java with hibernate?

I try to get all data in table(SELECT * FROM) mysql database, Using real time search, But not load data to table.
I called getAll("") method with empty String, But I can't get any value from my table.
public ArrayList<Titem> getAll(String text) throws SQLException, ClassNotFoundException {
Session session = HibernateUtil.openSession();
Query query = session.createQuery("FROM titem WHERE id = :id");
query.setParameter("id",text);
List<Titem> list = query.list();
ArrayList<Titem> entityList = new ArrayList<>();
for (Titem t:list
) {
Titem titem = new Titem(
t.getId(),
t.getName(),
t.getPrice()
);
entityList.add(titem);
}
return entityList;
}
You need to conditionally remove the where statement to stop filtering on that column, empty string or null values will also be matched against the data in the table. Or you could change the query to:
FROM titem WHERE (id = :id OR :id = '')

"select new constructor" query returns java.lang.Object instead of instance of constructors class

var toShow is instance of pure java.lang.Object, and the only way it works if I cast it to FactionProjection, and I do not think that's the proper way. My method:
public FactionProjection getFactionById(int id){
Query query = entityManager.createQuery(
"SELECT new com.gamerecords.result_recorder.model.projection.FactionProjection(f.name)" +
"FROM Faction f WHERE f.id = :id",FactionProjection.class)
.setParameter("id",id);
var toShow = query.getSingleResult();
return toShow;
}
constructor:
public FactionProjection(String name) {
this.name = name;
}
what I have tried:
changing second argument of .createQuery to Faction.class, or
deleting it
changing method .getSingleResult() to .getResultList()
Working code I have now is:
public FactionProjection getFactionById(int id){
Query query = entityManager.createQuery(
"SELECT new com.gamerecords.result_recorder.model.projection.FactionProjection(f.name)" +
" FROM Faction f WHERE f.id = :id",FactionProjection.class)
.setParameter("id",id);
var toShow = (FactionProjection)query.getSingleResult();
return toShow;
}
I have seen many examples of "select new constructor" expression, and there was no need for casting, which makes me think I'm making a mistake here.
The normal Query API does not return the exact type of Object you expect and you need to cast. Query#getSingleResult() returns an Object. You can use TypedQuery<T> instead:
TypedQuery<FactionProjection> query;
var result= query.getSingleResult();

Hibernate: Criteria & createSQLQuery: how to get proper JSON?

When I try this I get the proper JSON as a result, but it takes a lot of time:
Criteria c = sessionFactory.getCurrentSession().createCriteria(User.class);
List<User> users = c.list();
List<User> specialUsers = new ArrayList<>();
for (User user : users) {
List<Perm> userPerms = user.getProfile().getPerms();
for (Perm perm : userPerms) {
if (perm.getId().equals(SPECIAL_ID)) {
specialUsers.add(user);
}
}
}
return specialUsers;
and the JSON is like:
[{"id":111,"name":"Name111"},{"id":222,"name":"Name222"}]
In attempt to improve performance I tried code below. In SQL app the results are OK, a few records of users:
String sql = "SELECT u.id, u.name FROM app.user u inner join app.perms p where u.profile = p.profile AND p.right= :rightId";
List<User> specialUsers= (List<User>)sessionFactory.getCurrentSession()
.createSQLQuery(sql)
.setParameter("rightId", SPECIAL_ID)
.list();
return specialUsers;
Now the 'JSON' however looks like this:
[[111,"Name111"],[222,"Name222"]]
I tried several things, like select *, criteria.add(Restrictions...) but to no effect. What I noticed is that in the first case specialUsers.toString returns proper data, in the second case it returns meaningless Strings like Ljava.lang.Object;#23e1469f.
Any hints how to solve this?
I managed to solve this in this way, may not be perfect:
// get ids of all special users
String sql = "SELECT u.id FROM app.user u inner join app.perms p where u.profile = p.profile AND p.right= :rightId";
List<Integer> intIds = sessionFactory.getCurrentSession()
.createSQLQuery(sql)
.setParameter("rightId", SPECIAL_ID)
.list();
// convert to long values
List<Long> longIds = intIds.stream()
.mapToLong(Integer::longValue)
.boxed().collect(Collectors.toList());
// get all special users
Criteria c = sessionFactory
.getCurrentSession()
.createCriteria(User.class)
.add(Restrictions.in("id", longIds));
List<User> specialUsers = c.list();
return specialUsers;
}

How should I pass values into JPQL query?

I'm working on test, that will check if object had been saved into database. Test will save object with unique values and after that I want to fetch this object from database and check if it is not null. Id is auto generated, so I need to find this object by name.
My test looks like this:
#Test
public void shouldAddNewEmployee(){
String firstNameTest = "Jon";
String lastNameTest = "Doe";
double salary1test = 10.0;
String salary2test = "10.00";
String localityTest = "LosAngeles";
String zipCodeTest = "00-000";
String streetTest = "LosAngeles str.";
int streetNumberTest = 10;
Employee testEmployee = new Employee(
firstNameTest,
lastNameTest,
salary1test,
salary2test,
localityTest,
zipCodeTest,
streetTest,
streetNumberTest);
EmployeeRepository.addNewEmployeeFromCLI(testEmployee);
String customQueryFindJonDoe = "select e from Employee e where e.firstName = Jon and e.lastName = Doe";
EntityManager entityManager = JPAUtils.openEntityManager();
List<Employee> foundJonDoe = entityManager.createQuery(customQueryFindJonDoe).getResultList();
Assertions.assertNotEquals(null, foundJonDoe);
if (entityManager.isOpen()) {
entityManager.close();
}
}
after running it I get error:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Jon' in 'where clause'
How should this query look like?
You have to set the parameters on the query:
String customQueryFindJonDoe =
"select e from Employee e where e.firstName = :firstname and e.lastName = :lastName";
EntityManager entityManager = JPAUtils.openEntityManager();
TypedQuery<Employee> q = entityManager.createQuery(customQueryFindJonDoe, Employee.class);
q.setParameter("firstname", "Jon");
q.setParameter("lastName", "Doe");
List<Employee> foundJonDoe = q.getResultList();
More about querying you can find here:
https://docs.oracle.com/javaee/7/tutorial/persistence-querylanguage002.htm#BNBRG
https://www.objectdb.com/java/jpa/query
Or you buy a book
https://www.apress.com/de/book/9781484234198

jpa native query control entity(part 3)

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!

Categories

Resources