I am using JDO on google app engine. Each 'Employee' has a 'key'. I have a set of keys and wanted to retrieve all Employees whose key belongs to this set.
So I implemented it using the 'contains()' filter as specified here. The code works fine and looks like this -
List<Key> keys = getLookupKeys(....) ..//Get keys from somewhere.
Query query = pm.newQuery(Employee.class,":p.contains(key)"); //What is ":P" here?
List<Employee> employees = (List<Employee>) q.execute(keys); //This correctly gives me all I want
All that I wonder is what is this ":P" in this query? The Employee object does not have any field named 'p' neither my query declares any such parameter. So what does this 'p' point to? Does 'p' has any special meaning?
I believe it's mapping an implicit input parameter. As there's only one parameter, you don't need to explicitly call setParameter, you can just use it. I believe it would have been okay as:
Query query = pm.newQuery(Employee.class,":keys.contains(key)");
List<Employee> employees = (List<Employee>) q.execute(keys);
which might be clearer.
See the "implicit parameters" part of the Apache JDOQL docs for another example.
Related
I'm a Spring backend learner, currently want to query some data from mysql. What I want is just a field from table.(I want categoryName in category).
First, I was using LambdaQueryWrapper. I wrote
categoryLambdaQueryWrapper.eq(Category::getId, categoryId);
Category category = categoryService.getOne(categoryLambdaQueryWrapper);
But category values null occasionally.
Then, I changed to simply using getById method in categoryService, everything worked fine.
Category category = categoryService.getById(categoryId);
I confused, is there anything difference between the 2 ways of querying? I thought they were equivalent before. Thank you for everyone who can help me.
The difference is that categoryService.getOne(categoryLambdaQueryWrapper) returns a single entity that matches the conditions specified in the lambda query wrapper, or null if no such entity exists.
On the other hand, categoryService.getById(categoryId) directly queries the database for the entity with the specified categoryId, returns the entity if it exists, or throws an exception if it doesn't.
So if you want to ensure that a category exists before processing it, it's better to use categoryService.getById(categoryId).
What is the difference between FROM and SELECT clause in HQL.Is there any semantic difference in terms of results?
I googled, but could not find any justifying answer
The Marko's anwser is complete but you can read examples from the official Hibernate documentation.
Select clause : return selected objects in a query result set
From clause : return class instances
It depends on your needs but you don't need to specify select clause in your HQL queries.
These two queries are semantically quite discinct:
a FROM query asks for a complete object and the return value is accordingly one or more instances of the persistent class;
a SELECT query asks for a projection of the entity and in the basic case the return value is one or more arrays of values being asked for. You can also order the values being put into a map, list, or a dedicated DTO object using the form select new map/list/className.
I know that entityManager.find() method can be used to fetch a row with the primary key.
But I have a requirement to define a common find method which accepts a map having the where clause conditions. In the map, key will be the column name and value will be the column name value of the where clause. This method should return the list of selected rows.
Can some one help me out?
Take a look at: EntityManager.createQuery. If I understand your question correctly, this will allow you to create the query that you would like to execute. You could also, take a look at using a CriteriaBuilder.
find fetches the row respect to the primary key.Now as you want
"common find method which accepts a map having the where clause conditions. In the map, key will be the column name and value will be the column name value of the where clause. This method should return the list of selected rows"
for this you have to go for CriteriaQuery like this :
The following simple Criteria query returns all instances of the Pet entity in the data source:
EntityManager em = ...;
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Pet> cq = cb.createQuery(Pet.class);
Root<Pet> pet = cq.from(Pet.class);
cq.select(pet);
TypedQuery<Pet> q = em.createQuery(cq);
List<Pet> allPets = q.getResultList();
The equivalent JPQL query is:
SELECT p FROM Pet p
Moreover i will advise you to go for annotation based mapping in your entities & look for setter & getter for the methods.In that design also you can have customized method by java logic for this go here to read.
Following might be overkill, but is a fairly generic way to approach it utilizing the criteriaBuilder. While I can't paste the code here (work) I created an abstract BaseFilter<Entity> class.
The easy part is then having the implementing objects provide getXX, setXX properties. A getPredicates() was then added to return an ArrayList of predicates that the abstract BaseDAO could then invoke to perform the query.
We worked specifically with the getXX and setXXX so we could reference the elements via eg, get(Entity1_.childObject).get(ChildObject_.grandChildObject) to assist in refactoring. JPA also supports it via string name so you could implement the getPredicates with that.
JPA requires the actual Entity.class reference in their calls it was a bit of fun trying to obtain it. Eventually a google search turned it up.
I would like to know the difference between on these methods.
When use the createQuery()and find() methods of EntityManager ?
What the advantages about each of them ?
Thank you for answer me.
You use find when you want to look up an entity by primary key. That means you know exactly what you're looking for, you just want to pull it out of the database.
You use createQuery when you want to find entities using criteria or if you want to use a JPQL statement to define what you get back. So you would use the query when you want to get an entity or collection of entities matching some conditions.
The createQuery method allows you to create a JPQL statement that will be executed. The JPQL statement allowed is much more dynamic than the one executed by find. For example given the following table:
create table CAT(
cat_id integer,
cat_name varchar(40)
)
You could execute a query to find the cat by name.
entityManager.createQuery("select c from Cat c where c.name = :name");
The find method only allows you to retreive an object using its primary key. So to use the find method for the above table:
entityManager.find(Cat.class, new Integer(1));
In a nutshell, createQuery allows you to retrieve entities in a more dynamic fashion, while find limits you to searching for an entity with a known id.
I'd like to use Hibernate's Criteria API for precisely what everybody says is probably its most likely use case, applying complex search criteria. Problem is, the table that I want to query against is not composed entirely of primitive values, but partially from other objects, and I need to query against those object's id's.
I found this article from 2 years ago that suggests it's not possible. Here's how I tried it to no avail, there are other aspect of Hibernate where I know of where this sort of dot notation is supported within string literals to indicate object nesting.
if (!lookupBean.getCompanyInput().equals("")) {
criteria.add(Restrictions.like("company.company", lookupBean.getCompanyInput() + "%"));
}
EDIT:
Here's my correctly factored code for accomplishing what I was trying above, using the suggestion from the first answer below; note that I am even using an additional createCriteria call to order on an attribute in yet another associated object/table:
if (!lookupBean.getCompanyValue().equals("")) {
criteria.createCriteria("company").add(
Restrictions.like("company", lookupBean.getCompanyValue() + "%"));
}
List<TrailerDetail> tdList =
criteria.createCriteria("location").addOrder(Order.asc("location")).list();
Not entirely sure I follow your example, but it's certainly possible to specify filter conditions on an associated entity, simply by nesting Criteria objects to form a tree. For example, if I have an entity called Order with a many-to-one relationship to a User entity, I can find all orders for a user named Fred with a query like this:
List<Order> orders = session.createCriteria(Order.class)
.createCriteria("user")
.add(eq("name", "fred"))
.list();
If you're talking about an entity that has a relationship to itself, that should work as well. You can also replace "name" with "id" if you need to filter on the ID of an associated object.