Writing a query with ORMLite
Asked Answered
O

1

10

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

Ocampo answered 10/8, 2011 at 18:58 Comment(0)
L
27

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.

Lastly answered 10/8, 2011 at 22:6 Comment(5)
I 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);Corroboree
What @SorinPenteleiciuc? I don't see any order-by phrases in the original query. And does that really warrant a downvote?Lastly
The 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 ORMLiteCorroboree
He 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/springLastly
And it still doesn't explain the downvote.Lastly

© 2022 - 2024 — McMap. All rights reserved.