Writing a query with ORMLite
How can I write a query with ormlite instead of using .create or any other thing like that?
Goodness, there are tons of documentation about how to do this on the ORMLite site. Here's the section on the query builder.
I'm not sure what you mean by "full query" but your example will work with some tweaks:
List<...> results = newDao.queryBuilder().where().eq("name",valueofname).query();
It does not make sense to just return the name since the Dao hierarchy is designed to return the specific Client
object. If you just want the name the you can specify the name column only to return:
... clientDao.queryBuilder().selectColumns("name").where()...
That will return a list of Client
objects with just the name field (and the id field if it exists) extracted from the database.
If you just want the name strings then you can use the RawResults
feature.
![Majid](https://i.stack.imgur.com/mzbJf.jpg?s=256&g=1)
Comments
-
Majid over 3 years
How can I write a query with ormlite instead of using .create or any other thing like that? Can you please show me how for this simple example :
SELECT name FROM client
EDIT since I can't answer myself : I guess I had to search a little more , anyway I found how to do it with the QueryBuilder like this :
newDao.query(newDao.queryBuilder().where.eq("name",valueofname)
If someone knows how to write the full query that would be great , otherwise , I'll stick with this solution
-
Sorin Penteleiciuc over 3 yearsI think what the original question was something similar to : @Query("SELECT u FROM UserEntity u WHERE u.userId = :userId ORDER BY u.userId") List<UserEntity> findAllById(@Param("userId") Integer userId);
-
Gray over 3 yearsWhat @SorinPenteleiciuc? I don't see any order-by phrases in the original query. And does that really warrant a downvote?
-
Sorin Penteleiciuc over 3 yearsThe idea is that he would like to use it without the query builder in a similar fashion as spring data. There are example with query builder that I was also able to find but the idea is that I would like to keep using the spring-data but exchange Hibernate with ORMLite
-
Gray over 3 yearsHe doesn't mention spring in his question. Sounds like you are conflating your interests with the OP. Here's docs on ORMLite and spring: ormlite.com/docs/spring
-
Gray over 3 yearsAnd it still doesn't explain the downvote.