In hibernate Criteria specification, if I want to group property in table, I must use Projections.groupProperty. The problem is I can't select all the field in the table.
What I want to is:
SELECT * FROM entity GROUP BY field
If i use group in hibernate Criteria, then the groupProperty must be exist, and the sql result will be:
SELECT field FROM entity GROUP BY field.
Is there any way to get what I want? I see in hibernate code, in CriteriaJoinWalker, when I want to select all (select *), the code is already specify if there is projection or not. My assumption if I want to change the behavior, I must change the CriteriaJoinWalker code. I can modify the code, and maybe I can get the result what I want, but if I can, I do not want to override the hibernate core code to my project. Is there any way to change it directly without change hibernate code?
// CriteriaJoinWalker
if(translator.hasProjection())
{
resultTypes = translator.getProjectedTypes();
initProjection(translator.getSelect(), translator.getWhereCondition(), translator.getOrderBy(), translator.getGroupBy(), LockMode.NONE);
} else
{
resultTypes = (new Type[] {
TypeFactory.manyToOne(persister.getEntityName())
});
initAll(translator.getWhereCondition(), translator.getOrderBy(), LockMode.NONE);
}
Thanks
If you use Projections then you will not be able to get an Entity object as a whole. Unfortunately hibernate cant help you. Check out this Issue and also check out this thread.
Related
I want to filter entity by using #Where and want to know how to pass dynamic value in JPA
Example : #Where(clause = "name = 'Alas'") //Here 'Alas' is static i want to make it dynamic
You cannot make the structure of the WHERE clause itself dynamic in this way. Assuming you want to make certain portions of the WHERE clause dynamic, you could use:
SELECT *
FROM yourTable
WHERE name = ? OR ? IS NULL;
To the ? placeholders above, you would bind 'Alas'. If you don't pass any value, then the crtierion on name would simply be ignored.
I want to get all the values from a particular column in JPA and store all values into a list. currently, I am using the below approach but I am getting records in something else format.can someone please help me out
Query q1 = factory.createNativeQuery("select * from booking_attendee where booking_id="+id);
List<String> em1=q1.getResultList();
return em1;
query otput
em=[[Ljava.lang.Object;#68606667, [Ljava.lang.Object;#2cd7f99a, [Ljava.lang.Object;#137a5a5, [Ljava.lang.Object;#a45cc1c, [Ljava.lang.Object;#61fdc06d, [Ljava.lang.Object;#72f5eee1, [Ljava.lang.Object;#4e536797]
If you want to create a native query for this, it is more about how to solve this in SQL. You do not say SELECT * which means all columns. You would have to say SELECT your_column_name to select only a specific column.
Query q1 = factory.createNativeQuery("SELECT your_column FROM booking_attendee");
List<String> em1 = q1.getResultList();
The WHERE clause could and should be defined with the parameter binding of JPA. There are several advantages concerning performance and SQL injection.
Named parameter binding is special to the persistence provider (e.g. Hibernate). The common way for JPA is using ? to let your code be portable to other providers.
Query q1 = factory.createNativeQuery("SELECT your_column FROM booking_attendee b WHERE b.booking_id = ?");
q1.setParameter(1, id);
List<String> em1 = q1.getResultList();
Native queries offer the possibilities to use original SQL. Like this, some features which are specific for your database could be used with this. Nevertheless, if you do not have very specific SQL code, you should also have a look in JPQL, the specific query language of JPA, and the JPA Criteria API which offers advantages when you want to refactor your code, shows errors during compile time and makes the dynamic creation of queries easier.
Is possible to remove a clause (the order by clause in my case) from a dynamically constructed query in jOOQ.
Suppose that after creating the query:
DSLContext create = DSL.using(SQLDialect.POSTGRES);
SelectQuery<Record> query = create.select().from("myTable").where("fk = 1").orderBy(DSL.field("pk")).getQuery();
System.out.println(query.getSQL());
// select * from myTable where (fk = 1) order by pk asc
I want to change the order by clause or remove it to get only
select * from myTable where (fk = 1)
Is possible make this with jOOQ?. If is not possible and anyone knows a query builder library that allows this will be also welcome.
This currently cannot be done through the public API. In a future jOOQ 4.0, there might be a cleaner separation of the DSL API and the Model API, where such a model API would allow you to freely manipulate all your query parts, including removing objects again from SELECT clauses.
Right now, you have at least two options to implement dynamic SQL:
Don't add the ORDER BY clause until you know whether you need it:
SelectQuery<?> select = create
.select()
.from("myTable")
.where("fk = 1")
.getQuery();
if (someCondition)
select.addOrderBy(DSL.field("pk"));
Alternative piece of logic:
List<SortField<?>> orderBy = new ArrayList<>();
if (someCondition)
orderBy.add(DSL.field("pk").asc());
create.select()
.from("myTable")
.where("fk = 1")
.orderBy(orderBy);
Post-process / transform your query using an ExecuteListener or a VisitListener. This is more of a workaround in edge-cases, though.
In your particular case, you should probably go with option 1. Another, more functional approach to tackling dynamic SQL generation is documented here.
I am trying to search a view based on given criteria. This view has a few fields for multiple different entities in my application that a user may want to search for.
When I enter the name of an entity I want to search for, I add a restriction for the name field to the detached criteria before calling .findByCriteria(). This causes .findByCriteria() to retrieve a list of results with the name I am looking for.
Also, when I look through my log, I can see hibernate calling a select statment.
I have now added another entity to my view, with a few searchable fields. When I try to search for a field related to this new entity, I get an exception in my log.
When I look through my log with the exception, I can see hibernate calling a select statment with an update statement right after the select (I am not trying to update a record, just retrieve it in a list).
So why is hibernate calling an update when I am calling .findByCriteria() for my new entity?
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
SQL that is executed:
Hibernate:
select
*
from
( select
this_.SEARCH_ID as SEARCH1_35_0_,
this_.ST_NM as ST24_35_0_
from
SEARCH_RESULT this_
where
this_.LOAN_TYPE=? )
where
rownum <= ?
DEBUG 2012-03-21 11:37:19,332 142195 (http-8181-3:org.springframework.orm.hibernate3.HibernateTemplate):
[org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:389)]
Eagerly flushing Hibernate session
DEBUG 2012-03-21 11:37:19,384 142247 (http-8181-3:org.hibernate.SQL):
[org.hibernate.jdbc.util.SQLStatementLogger.logStatement(SQLStatementLogger.java:111)]
update
SEARCH_RESULT
set
ADDR_LINE1=?,
ASSGND_REGION=?,
BASE_DEAL_ID=?,
ST_NM=?
where
SEARCH_ID=?
There is probably an update happening because Hibernate is set up to do an autoflush before executing the queries, so if the persistence context thinks it has dirty data, it will try to update it. Without seeing the code I can't be sure, but I'd guess that even though search_result is a view, your corresponding Java object is annotated on the getters and the object has matching setters. Hibernate doesn't make a distinction between tables and views, and if you call a setter, Hibernate will assume that it has data changes to update.
You can tweak how you build your Java objects for views by adding the #Immutable annotation (or hibernate.#Entity(mutable = false) depending on which version you're using. This should be enough to indicate to Hibernate to not flush changes. You can also annotate the fields directly and get rid of your setters so that consumers of the SearchResult object know that it's read only.
I have a object A which maps to table A in DB
class A {
Integer id;
String field2,field2;field3 ,... fieldN;
//lots of other attribute
}
Now i want to write a DAO api that just updates a single field.One approach is that i can first load the object then changes the attribute i need and then use merge api
//start transcation
A a = session.load(A.class, id);
A.setfieldP(newValue)
session.merge(A)
//commit transcation
Now if i use following code
//start transcation
A a = new A();
a.setId(id); //set a id by which object A exists in DB
A.setfieldP(newValue)
session.merge(A)
//commit transaction
Now second approach all fields except id and fieldP are set to null
1)Now is there any other approach?
2)Can i use update instead of merge ?
If you need to update lots of entities at once the most efficient way is to use a query:
Query query = session.createQuery("update EntityName set fieldP = 'newValue' "
+ "where id IN (75, 76)");
query.executeUpdate();
This allows you to change field values without loading the entity or entities into memory.
It is best practice is to use named queries and named parameters - the above implementation is just an example.
I usually prefer session.get vs session.load, as session.get will return null as opposed to throwing an exception, but it depends on the behavior you want.
loading the object, setting your field, and calling either
session.merge(myObject)
is the standard way, although you can also use
session.saveOrUpdate(myObject)
as long as the object hasn't been detached, which in your case, it won't have been detached. Here is a good article explaining the differences in merge and saveOrUpdate.
In your second example, you are editing the primary key of the object? This is generally bad form, you should delete and insert instead of changing the primary key.
Using JPA you can do it this way.
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaUpdate<User> criteria = builder.createCriteriaUpdate(User.class);
Root<User> root = criteria.from(User.class);
criteria.set(root.get("fname"), user.getName());
criteria.set(root.get("lname"), user.getlastName());
criteria.where(builder.equal(root.get("id"), user.getId()));
session.createQuery(criteria).executeUpdate();
One more optimization here could be using dynamic-update set to true for the entity. This will make sure that whenever there is an update, only field(s) which are changed only gets updated.