Is there any specific method for finding record from database?
I don't want to use criteria api and load() only fetch data using primary key but what about if i want to find records using other columns? I know it is possible with criteria but want to know is there any other option?
From our discussion in chat. OP was looking for a way to query using combinations of entity fields only known at runtime. I've suggested Criteria - Example Queries.
Sample from official documentation:
Cat cat = new Cat();
cat.setSex('F');
cat.setColor(Color.BLACK);
List results = session.createCriteria(Cat.class)
.add( Example.create(cat) )
.list();
As far As I know there is no method directly for that ..
You can try using HQL or Native SQL query.
And
In criteria API you need to add Projections to fetch the required properties on POJO's
Related
I don't want to show all my attributes on a table, so I'd like to select only a few columns and then map their values to a bean, something similar to Hibernate's Transformers.aliasToBean(T.class), but using JPA and Criteria.
I'm able to get a List<Tuple>, how could I transform it into a List<T>?
As the the title says, is it possible?
Within JPQL/Criteria queries you can provide arguments to an objects constructor within the query so that the query returns those objects for you. For example:
"SELECT new package.BeanClass(e.attribute1, e.attribute2) from Entity e"
With native SQL queries, if you are using JPA 2.1, you can use ConstructorResult to build the same instances:
http://en.wikibooks.org/wiki/Java_Persistence/Querying#ConstructorResult_.28JPA_2.1.29
But if you are not able to use Jpa 2.1, you will need a provider specific solution such as http://onpersistence.blogspot.com/2010/07/eclipselink-jpa-native-constructor.html
I was reading hibernate criteria document here:
http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/Criteria.html
I had used it many time and I am normally use createAlias() to join table, here they have provide two methods to make a join and fetch data from the tables, there are:
List cats = session.createCriteria(Cat.class)
.createCriteria("kittens")
.add( Restrictions.like("name", "Iz%") )
.list();
List cats = session.createCriteria(Cat.class)
.createAlias("kittens", "kit")
.add( Restrictions.like("kit.name", "Iz%") )
.list();
So I am not able to distinguish difference between .createCriteria("kittens") and createAlias("kittens", "kit") or may be I am not getting what this code exactly do, can someone help me to clear my confusion.
The only difference is that CreateCriteria has 2 additional
overloads without the alias parameter, this difference is long gone in
the latest versions.
But essentially the application is slightly
different in its usage is that CreateCriteria uses its relations of
mapping from parent to child, whilst with CreateAlias you defined them
with your customized alias names from the root.
Read more from here.
Main Difference is that Criterias' createCriteria() creates and returns Sub Criteria (new Criteria Object).This is useful if you want to create criteria for subquery.
Here is what documentation says about its return type
Returns:
the created "sub criteria"
Criteria's CreateAlias() returns existing Criteria Object
Here is what documentation says about its return type
Returns:
this (for method chaining)
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.
Example:
List<Object[]> list = em.createQuery(
"SELECT 'Foo', 123 FROM IrcEvent ev", Object[].class).getResultList();
What I don't like in that example:
How do I know the table name? Can't I specify the entity class instead?
How do I know the column name? jOOQ provides auto-completion by creating a DSL from the database schema.
There could be syntax errors everywhere.
What I basically want is something like
entityManager.deleteAll(EntityClass.class);
to delete the rows of an antire table (for example).
JPA 2 Criteria API http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html is for creating type safe queries programmatically, but it does not support deletes
I would like to ask if it's possible to do this using hibernate.
Let say I have already run a HQL and retrieved a collection. Is it possible to further filter it using hibernate?
I tried to use the <filter> to the header class and add session.enable() before the query, but seems it's not working.
Sample code
Query search = session.getNamedQuery(HQL_SOMEDEFAULTQUERY);
List results = search.list();
//further filtering ...
Stripped down HQL
select h
from flow as f
join f.item as i
join i.header as h
where i.status = :status
and f.staff = :staff
order by i.prId desc
No. At least, not the way you asked. Once you ask Hibernate to hit the database (with the list() method), Hibernate did its part and the results are now in your hands. You can implement a filtering logic in your code to post-process the results.
That said, it is possible to filter the results in the query itself. If you define a Hibernate filter and enable it for a specific model/query, you'd be able to keep your original HQL query and Hibernate will append it with extra where clauses to further filter the results. See this:
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/filters.html
The better way would be to use Criteria. Here is an example from Hibernate Documentation that explains usage of Criteria.
Criteria would be used before you call list method.
Hope that helps.