Questions about query several tables through JPA - sql

Recently, I need to use JPA in my project.
However, I encountered some difficulties. JPA seemes to be harder than I thought.
So, I post my problem here, hoping that, some guys may help me.
There are two tables operation and analysis, the sql is like the following:
select op_id from operation where flag = 1
union
select an_id from analysis where on_type = "processed";
op_id and an_id are two column names actually means the same.
Can anyone help me writing a JPA version of this?
I'm very grateful of that.
Thank you very much.

JPQL does not support unions.
You have four options:
You can create a View in your DB that would do the unioning, then map to that view
Write a native SQL that does the union (since you only need the ID, this is a valid option)
If the IDs are distinct, you can run two queries, and concatenate the result list (if not distinct, you could use Sets to filter duplications)
If you are using the latest (at least >=2.4) EclipseLink as a JPA provider, you can use UNION
See this question here.

Related

How to do 2 select queries?

I am using Firebird and what I want to do is display 2 different select queries. Here is an example
select * from tblStates;
select * from tblTeachers;
This are two tables with 2 completely different columns. When I use the code above firebird will only display tblTeachers. What I want is to see both tblStates and tblTeachers as two different tables. I was told to use suspend but I don't know the syntax and when I just type suspend there is a unknown token error.
I am unfamiliar with the details of Firebird. However, in doing some research, I came across this post that might help.
What you're looking for is considered a batch separator statement. In SQL Server, it would be something like:
SELECT * from myTable1
GO
SELECT * from myTable2
GO
This would return two tables in a table or database studio viewer. I did not see something similar for Firebird other than what is linked above.
However, the next question is why are you wanting this functionality? Are you sure there is not a relationship between States and Teachers, as per your example? If there is not, then a common practice would be to run your unrelated SQL statements and save the returned tables in memory for use in your application.
Sometimes, if you cannot figure out a way to do what you want, its a good idea to look back at exactly what your goal is and wonder if there might be a better way :)
Hope this helps.

I'm being asked to create IN queries for different GUIDs...huh?

I'm a GIS intern.
I've been asked:
"Could you also create IN queries for the different sets of GUID’s? Here is an example:
"GlobalID" IN '{58BEE03F-1656-4BD5-B53D-B887E93A5287}', '{009C7364-8D77-46B3-A531-B60ED4E5B407}', '{0105263C-1305-4AB9-A00A-4BED01832177}')"
I'm not sure what that means or why I'd have to do it. What I can tell you is that I have several .shp that I have geocoded and then created global IDs for.
I've googled this for hours now and am no closer to understanding the request than I was. It could be that the answer is staring me in the face but I don't think I know enough to know that.
Thank you,
Kathy
In order to create and understand IN queries, first you'll have to understand the basics of a query. It sounds like this might not be something you're familiar with, so I'll start with that.
There are 3 main parts to a query, SELECT, FROM, and WHERE.
SELECT is the information (or columns) you want to return. You can SELECT * to select all columns or SELECT specificColumn1, specificColumn2 to select specific columns.
The next step is the FROM statement. From determines what table(s) you will be querying. You can query multiple tables here if you like and tables can also be aliased like so: FROM table1 t1.
The third statement is the WHERE statement, which specifies any conditions that the query is required to meet. In your case, this is where your IN statement will go. There are a ton of different keywords you can use here, but I'll just give a quick sample query for you (keep in mind I have no idea what your schema looks like).
SELECT *
FROM GUIDData
WHERE GlobalID IN ('{58BEE03F-1656-4BD5-B53D-B887E93A5287}', '{009C7364-8D77-46B3-A531-B60ED4E5B407}', '{0105263C-1305-4AB9-A00A-4BED01832177}');
So what this query will do, is it will give you all the data for each item in the GUIDData table with a global ID of {58BEE03F-1656-4BD5-B53D-B887E93A5287}, {009C7364-8D77-46B3-A531-B60ED4E5B407}, or {0105263C-1305-4AB9-A00A-4BED01832177}.
Did this help?

How to write riak query in riakc

I am a noob at riak, and have been trying to test the query aspect of riak using riakc in erlang. but I can not find any example of how to query the database that match the old SQL way, Only on how to get a single value out of a single field. I think I am missing something but all I really want is just a standard SQL query with a matching riakc code.
SELECT * FROM bucket;
SELECT * FROM bucket LIMIT 10, 100;
SELECT id, name FROM bucket;
SELECT * FROM bucket WHERE name="john" AND surname LIKE "Ste%";
SELECT * FROM bucket LEFT JOIN bucket2 ON bucket.id = bucket2.id2;
I assume that there is no direct correlation on how you write these, but was hoping there is a standard way, and is there somewhere that has a a simple to understand way of explaining this querys in riakc (or even just riak).
I have looked a mapreduce but found that it was confusing for just simple queries
Riak is a NoSQL database, more specifically a key-value database, and there is no query language like SQL available. When working with Riak you need to model and query your data in a completely different way compared to how you use a relational database in order to get the most from it. Trying to model and query your data in a relational manner, e.g. by extensive use of secondary indexes or by trying to use map/reduce as a real-time query language, generally results in very poor performance and scalability. A good and useful discussion about Riak development anti-patterns that can be found here.

nhibernate and window functions

Is there any way to get NHibernate to use a Window function?
Specifically, I'm looking to create a query like the following:
SELECT
date,
SUM(Price) OVER (ORDER BY date)
FROM purchases
GROUP BY date
I've Search the web and can't find anything about window functions and NHibernate
Specifically, I'm using the QueryOver interface for NHibernate.
If it's not possible, what other solutions are there to keep database independence in my code?
Good question but unfortunately I've never seen any code within NHibernate itself that would allow it to generate such a window function.
Your only option would be to use a named query. In your named query you could either type out the SQL by hand or call a stored procedure.
You stated that you wanted to keep your solution database independent. I've never personally cared much doing such as thing as I always work with SQL Server but I can understand where you're coming from. I've heard of several people who run their integration tests with an in memory SQLite solution and then run their actual code on SQL Server or Oracle.
In your scenario, I don't believe the SQL in your question is ANSI compliant anyway so you'd have to re-write it in a different way unfortunately if you used a named query to make it database independent.
As far as I know, you can't do that with the QueryOver API.
What I would do to keep as DB independant as possible would be to consider the windowed query as a join between your table and a group by query. Then to write a QueryOver which would lead this join.
Considering you query it would lead to this : (though the example does not seem to really show the need for a windowed query)
SELECT
purchases.date,
purchasesGroup.priceSum
FROM
purchases
INNER JOIN
(SELECT date, SUM(Price) as priceSum FROM purchases GROUP BY date)
AS purchasesGroup
on purchases.date = purchasesGroup.date
You could potentially use a view for this. That could at least abstract away a little bit of it, although you are really migrating the dependency to the database schema.
I've run into this kind of issue as well lately, and I'm starting to realize that I am often trying to use NHibernate in a way that isn't really about persisting entities. If I were Ayende Rahien I would probably know what to do, but since I am not I usually end up mixing in a little Dapper when I just need something like a read-only aggregate result such as statistics about data.

How do you do a union of two tables in NHibernate?

I need to do a union of two tables using NHibernate and HQL. I have found very little help online, and I want to know if it is possible and if so how?
Found my answer:
http://www.hibernate.org/117.html#A21
It doesn't currently support union or intersect.
You could use a named sql-query and do the union in raw SQL. NHibernate will be able to populate entity instances from the sql-query and return those as the query result. See here and here.
I don't believe HQL supports unions, but you can write your own record transformer and author the sql by hand.