Does OpenJpa return resultlist in any particular order - openjpa

does openjpa return resultlist in a particular order even if Order by clause is not mentioned in query??
i m getting the result in ascending order and have not written order by clause.

<property name="openjpa.Log" value="SQL=trace"/>
You could always enable trace to see what SQL is being generated.

Related

Spring Data: Getting NonUniqueResult Problem for the query

Hello experts of the world. Need some help concerning executing a query with SpringData.
The expectation is to execute the Query below in the Spring Data annotation by combining with the repository method name (Automated Query Construction) to get a unique result. Apparently it fails from time to time by saying the result is not Unique.
The question here is if the method name is still considered in Query Construction while also executing the query in the annotation.
#Query("SELECT r from Revision r WHERE r.revisionBid = ?1 AND r.revisionStatusId = ?2 ORDER BY r.lastModifiedDate DESC")
Optional<Revision> findFirst(Integer revisionBid, Integer revisionStatusId);
Thanks in advance!
The query creation for limiting to 1 result is defined here with FIRST & TOP included in the method name.
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation
I don't think "findFirst" will work when you're using an #Query, as the query will be constructed from HQL expression in the #Query rather than the fluent API passing over the method name. Because of this, when the query returns multiple results, it will throw the exception as the Optional is told to wrap a single returned object, not a collection. Add a LIMIT clause to the HQL query and you should be good.

How to check the multiple values against a same name attribute in xml column of DB2 using XMLQuery?

Here is my query which I'm using to fetch the data from my table.
SELECT XMLQUERY('$INFO/root/database_systems/system/#name = ("SYS1","SYS2","SYS3")')
FROM MyTable WHERE ACCT_ID = 'ID-1234';
Ok actually it is returning me true. Just because of the first value SYS1. It exists in the hierarchy but not the others. I just want to compare multiple values.
Please suggest a way to achieve this functionality. Thanks
<root>
<database_systems>
<system name="SYS1">1</system>
</database_systems>
</root>
If I understand correctly, you want to check for a database_systems element that contains at least all of the child elements:
<system name="SYS1">...</system>
<system name="SYS2">...</system>
<system name="SYS3">...</system>
If that is correct then you need to AND your conditions together, what you had previously was an OR:
SELECT XMLQUERY('not(empty($INFO/root/database_systems[system/#name eq "SYS1"][system/#name eq "SYS2"][system/#name eq "SYS3"]))')
FROM MyTable WHERE ACCT_ID = 'ID-1234';
I have used three predicates to achieve the AND, and then I check that a match was found using not(empty(...)). There are plenty of other ways to achieve this too.

Apache Camel JPA: read from multiple tables

I am new to camel-jpa and would need your help with the following problem:
I need to read data from a database table, transform it and save it into another database:
<route id="FromEmployee1ToEmployee2">
<from uri="jpa1://Employee1?consumeDelete=false&consumer.namedQuery=getAll" />
<bean ref="transformerBean"/>
<to uri="jpa2://Employee2"/>
</route>
This is already working great!
But the problem now is that I need to look up some data for Employee1 from a different table (I need to read the "last_modified" date for that entry). In SQL I would simply do like this: select last_modified from table2 where table2.id = <employee.ID>. but how can I realise this with camel-jpa?
The camel-jpa component offers options to use a query, such as a named query. This allows you to write in SQL like using the JPL (I think thats the JPA name for SQL).
There is a little example at
http://camel.apache.org/jpa
here is my solution (just in case someone is struggling with the same problem):
since its not possible to dynamically pass attributes to a named query in a camel route (at least, I couldn't find any way to do it...), i used a bean to deal with it:
<route...
<from uri="jpa1://Entity1" />
<bean ref="MyBean" />
<to uri="jpa2://Entity2" />
</route>
and within the bean, i use a (autowired) DAO to call my queries. This allows me to do all the kinds of content enrichment i need ...
well, it works great so far, but i think there could me a more elegant way of doing this with camel...
BR,
M

EclipseLink JPQL (Glassfish v3): join fetch syntax problem?

With Hibernate I'm used to do something like the following:
select n from NetworkElement n join fetch n.site s where s.active is true
However, EclipseLink complains a lot about this:
Caused by: Exception [EclipseLink-8024] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing the query [select n from NetworkElement n join fetch n.site s], line 1, column 49: syntax error at [s].
(The query on the stack is different from the one above, but the result is the same)
I've tried different combinations, none of which worked:
select n from NetworkElement n join fetch n.site where n.site.active is true
select n from NetworkElement n join fetch n.site as site where site.active is true
I also tried switching to a different entity in my domain model, suspecting that maybe my mapping is not correct. Still, the same problem.
Could it be that I can only achieve this using a query hint? I don't want to do that.
By the way, I'm using EcliseLink as shipped with Netbeans 6.8 and Glassfish v3.
I'd appreciate any help!
Rodrigo
The main issue is that the JPQL syntax does not allow for aliasing fetch joins and that is why EclipseLink uses query hints for this functionality. There is an Enhancement Request to add aliasing join fetches directly in the JPQL and if you would like to see it completed please vote for it. ( https://bugs.eclipse.org/bugs/show_bug.cgi?id=293775 ).
--Gordon Yorke
Well, it seems that one is not allowed to alias a fetch join in JPQL, indeed.. It works with Hibernate because it supports aliasing through HQL, and you are allowed to issue HQL to a JPA Query object.
Therefore, I had no choice but to switch to named queries with query hints. I don't really like declaring queries with annotations because of the high verbosity on the entity classes, so I added a orm.xml file to the persistence unit's jar, and did the following:
<!-- Fetch network elements -->
<named-query name="fetchNetworkElements">
<query>select n from NetworkElement n</query>
<lock-mode>NONE</lock-mode>
<hint name="eclipselink.join-fetch" value="n.site" />
<hint name="eclipselink.join-fetch" value="n.site.area" />
</named-query>
Hope this gives some clue to anybody struggling with the same shortcomings of the raw JPQL.

Does DataReader.NextResult retrieves the result is always the same order

I have a SELECT query that yields multiple results and do not have any ORDER BY clause.
If I execute this query multiple times and then iterate through results using DataReader.NextResult(), would I be guaranteed to get the results in the same order?
For e.g. if I execute the following query that return 199 rows:
SELECT * FROM products WHERE productid < 200
would I always get the first result with productid = 1 and so on?
As far as I have observed it always return the results in same order, but I cannot find any documentation for this behavior.
======================================
As per my research:
Check out this blog Conor vs. SQL. I actually wanted to ask if the query-result changes even if the data in table remains the same (i.e no update or delete). But it seems like in case of large table, when SQL server employees parallelism, the order can be different
First of all, to iterate the rows in a DataReader, you should call Read, not NextResult.
Calling NextResult will move to the next result set if your query has multiple SELECT statements.
To answer your question, you must not rely on this.
A query without an ORDER BY clause will return rows in SQL Server's default iteration order.
For small tables, this will usually be the order in which the rows were added, but this is not guaranteed and is liable to change at any time. For example, if the table is indexed or partitioned, the order will be different.
No, DataReader will return the results in the order they come back from SQL. If you don't specify an ORDER BY clause, that will be the order that they exist in the table.
It is possible, perhaps even likely that they will always return in the same order, but this isn't guaranteed. The order is determined by the queryplan (at least in SQL Server) on the database server. If something changes that queryplan, the order could change. You should always use ORDER BY if the order of results is in anyway important to your processing of the data.