0

How can i use jpa for query over an object (not an entity)?

For example this simple code:

String [] theList = {a,b,c,d}. 
Query q = new Query("Select tl from theList tl")

Reason behind: the queries are dynamically created and executed, but the objects in the from clause of the jpql query aren't necessarily mapped tables. In some cases there are just an Object, So the actual behavior needed is modify the query during execution of the program to meet the criteria, but i don't know how to modify the query.

Edit: I Don't use native queries because of portability of code. It will be the last option.

6
  • 2
    Why not a native query? Commented Feb 27, 2019 at 19:46
  • Thanks for suggestion. I have edited the Question. The reason is that behind the project there are several dbms (Oracle, Sybase, SqlServer and Postgres) and there are over 10000 queries, so much to maintain with Database-specifics, +1 for the suggest. Commented Feb 27, 2019 at 19:56
  • Sorry but can you put much details I don't get your question, what did you mean by but the objects in the from clause of the jpql query aren't necessarily mapped tables Commented Feb 27, 2019 at 20:03
  • You mean query in memory, or query in database (in which case you are not querying over an "object") ? Commented Feb 28, 2019 at 7:04
  • @YCF_L Literaly the expression. The Objects of the from clause of the query aren't mapped tables in some cases, but java objects. Commented Feb 28, 2019 at 15:26

3 Answers 3

1

What you're looking for is called LINQ, and unfortunately (?) it is available only in C#.

However, you can partially emulate it with Stream(s).
A Stream offers basically all the operators you need

.filter()               where
.max()                  max
.sorted()               orderby
.limit()                limit
.skip()                 offset
.collect(groupingBy())  group by

And so on. Just give a look at the Javadoc!

Sign up to request clarification or add additional context in comments.

4 Comments

I'll take a look, +1 for the suggestion. Actually i'm reading about eclipselink expressions but i'm unsure on how to do it.
@CesarLeonardoOchoaContreras but wait. The example shows an array you're operating on. Eclipselink expressions are for database accesses.
Oh yeah. This is because i'm dealing with distincts levels of subqueries (Remeber that jpa don't support subqueries in the FROM Clause, so i'm extracting and executing the subqueries to objects first and my purpose is to query then those objects.
@CesarLeonardoOchoaContreras Well, once you have your first result-set from JPA, if you don't need the latest data (which might be updated by other users in the database) you can operate via Streams.
0

I think 'JdbcTemplate' would suffice your requirement.

JdbcTemplate gives you the flexibility to run native queries and map them to a Java class.

However, you'll have to explicitly map your Java class with the column names in the database.

Comments

0

I have solved using joSQL. Is a powerfull opensource tool that allows you to query over java objects using "sql". It is not jpa but satisfied my needs. Another tool i have seen that do that is called querydsl.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.