Getting entity by list of ids in Appengine-Java - java

I have an appengine backend for an android app.
In this I want to retrieve a number of entities based on their 'id'
I've tried using a WHERE IN query for this as follows.
"select from MyEntity as MyEntity where id IN ('4538783999459328')"
The datastore contains a MyEntity with key that contains id as 4538783999459328.
But the result set is blank. I have tried using other field names of the entity and it works, but am not able to filter with the id.
Is there any other way of doing this?
I'm using Appengine JAVA.
I don't want to use EntityManager.find() because I want to look up more than one id.

Two things:
you have to use __key__ instead of id in queries
there is a special Data type for Entity id - Key('EntityName', ID)
So, your query should look like this:
select * from MyEntity where __key__ in (Key('MyEntity', 4538783999459328))
Also, take a look at GQL doc: https://cloud.google.com/appengine/docs/python/datastore/gqlreference

All the frameworks that implements datastore have a choice of get entities by a list of IDs (Key), or how the above post with GQL is the same.

Related

Access user defined method properties on query - Esper

I implemented a query where i mined data from a database but i have to change it so i mine my data from a custom function in my code. I read the documentation and added the annotation import on the configuration. The query throws that error:
Failed to resolve event type, named window or table by name 'path.to.my.class.customfunction'
I don't know the type that my function have to return but i tried Arraylist and Hashmaps with key an integer and value a custom class and didn't work.
My final query want to look like this :
select * from LocationEvent as loc,
***CustomFuntion()*** as product
where loc.id=product.id ;
I kept the structure i used for database connection. I don't know if there is another way to solve this. Thanks.
EDIT: I managed to make call the custom function with that query :
select path.to.class.getProducts() as product from pattern[every timer:interval(3 sec)]
My function right now return an ArrayList and the query returns this:
[Product{ProductID=124,.....,},Product{...}]
So now my problem is that i can't access properties of Product on the query like products.ProductID
If you want to have a custom function in the from-clause you can use the "method:". The docs have this described here: Accessing Non-Relational Data via Method. The Esper runtime then calls your method to get events/rows.

Mapping multiple hibernate Entities inside non-entity java bean

Hi I am using a spring boot app with Hibernate using Oracle as DB.
I have 5 classes named
1.Reqest -> Mapped with Request Table
2.Team -> Mapped with Team Table
3.Partner -Mapped with Partner Table
4.Customer -> Mapped with Customer Table
Now I want to Display a Summary of Request on summary screen of the app where all the information from above-mentioned tables is needed.
Suppose I create a bean class as follows.
public class SummaryBean{
Request req;
Team team;
Customer cust;
Partner part;
//Getter setters;
}
Now since I have all the tables mapped with Java classes I can use hql join query to fetch data.
I don't want use plain SQL query with join and then iterate the resulting Object[] list from hibernate query and stub data into SummaryBean manually.
All the above-mentioned tables have REQ_ID as joining column
My question is How can I make hibernate map the result of that query to SummaryBean object?
Is it possible at all?
You can use constructor query.
Something like
"select new SummaryBean(req, team, cust, part) from (here you join your tables)"
You need to provide a constructor for the SummaryBean with those 4 types.
Note that the SummaryBean class doesn't have to be mapped, but you might have to use fully qualified name in the query (packageName.className).

How can we query in hibernate which returns a Map object?

I have an entity Subscription which has the following properties
name,BillOfLading,validTo (Date),ValidFrom (Date)
Sample Data:
Name BillOfLading ValidTo ValidFrom
A 100101 1/28/2016 3/28/2017
A 100102 1/29/2016 3/29/2017
B 100103 1/30/2016 3/30/2017
C 100104 1/31/2016 3/31/2017
A 100105 1/28/2016 3/28/2017
Here I'm trying to get result in the form Map
Map<String,List<Subscription>>
The map contains a (key,value) pair,where Name as key and List of subscriptions that belongs to specified name.
Suppose the Name A has 3 records So now the key will be A and value will be list of Subscriptions that belongs to A.
And also I need to sort Map based on the count of A subscription there in DB.
Note: The above is a shortened story of my scenario. So, I'm not putting my actual code.
Please help me if there is any possibility of returning Map in hibernate.
I dont see being possible to return Map<String,List<Subscription>> by a JPQL/HQL query.
I had return Maps from JPQL for example like this:
#Query("SELECT new map(r.code, r.name) FROM Substance r GROUP BY r.code, r.name")
List<Map<String, String>> select();
If you need a that complex return type have a look at Hibernate's ResultTransformer interface and it's implementations, which can customize the result you want. I had never used this. There is a blog post which show the usage of this interface: Why you should use the Hibernate ResultTransformer to customize result set mappings

Hibernate Lazy Loaded Entites Id

Question, am currently using hibernate and I was wondering if there was any way to get the value of a lazy loaded entities id without hitting the DB again? For example we currently have an entity called group that has a ManyToOne relationship with another entity Organization.
when we call a simple repository method
groupsRepository.findByUser(id) it returns a list of groups based on given user. The query looks like this
select
groups0_.PersonId as PersonId1_118_1_,
groups0_.GroupId as GroupId2_8_1_,
groups1_.GroupID as GroupID1_74_0_,
groups1_.CreatedAt as CreatedA2_74_0_,
groups1_.CreatedBy as CreatedB3_74_0_,
groups1_.Description as Descript4_74_0_,
groups1_.Name as Name5_74_0_,
groups1_.OrganizationID as Organiza9_74_0_,
groups1_.Role as Role6_74_0_,
groups1_.Status as Status10_74_0_,
groups1_.TouchedAt as TouchedA7_74_0_,
groups1_.TouchedBy as TouchedB8_74_0_
from
Groupsofpeople groups0_
inner join
groups groups1_
on groups0_.GroupId=groups1_.GroupID
where
groups0_.PersonId=?
Later on I need to see if the groups org is equal to another org which I do by comparing the OrganizaitonId's, a value that has already been fetched from the db. But every time I do this I have fetch the org from the db, is there anyway to prevent that so that the org attached to group will be prepopulated with it's id since it is already being fetched anyway? Or is this just a concession I have to make if I am using hibernate?

How to bulk delete from element collection in jpa

I'm using jpa 2.0 and I have the following entity:
#Entity
public class Folder{
#ElementCollection
#CollectionTable(name="folder_files")
private Set<String> files;
// .....
}
Given a file name, I would like to delete all entries where files == theGivenFileName. In sql it would be something like this:
Delete from folder_files where files = XXX
Is there a way to perform this query using criteria-api?
If not, is there a way to perform this query using jpql?
UPDATE:
I think my question was not clear enough:
Since jpql uses entities (and not tables) I cannot just perform the sql written above plus since I'm using #ElementCollection I don't know how to address this variablr or even deal with it. I would like to delete all entries in that collection (in my case, the files set) which holds a given value, from all entities. Is that possible using jpql or (even better) criteria-api?
The Delete FROM clause requires an Entity, so there is no way to delete from an element collection from what I understand.
You can use a native SQL query, or you can map the element collection as a OneToMany to an Entity instead.
You can use the like query just the syntax is slightly changed.
query = em.createQuery("SELECT i FROM Item i WHERE UPPER(i.name) LIKE :keyword ");
query.setParameter("keyword", "%" + keyword.toUpperCase() + "%");
You can read more on following link,
https://forums.oracle.com/forums/thread.jspa?threadID=423742
Updated:
#Noam you can do it: Like in Criteria API
List cats = sess.createCriteria(Cat.class)
.add( Restrictions.like("name", "Fritz%") )
.add( Restrictions.between("weight", minWeight, maxWeight) )
.list();
Kindly read more on it at following link:
http://ctpconsulting.github.com/query/1.0.0.Alpha3/criteria.html
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html
This cannot be done. Via JPQL it does not work because DELETE statement can only be applied to entities. Excerpt from JPA 2.0 specification:
Bulk update and delete operations apply to entities of a single entity
class (together with its subclasses,if any).
...
delete_statement ::= delete_clause [where_clause]
delete_clause ::= DELETE FROM entity_name [[AS] identification_variable]
Also it doesn't work via Criteria API. CriteriaQuery supports only selecting - not updates or removals.
You have to go for native SQL.

Categories