Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I want to write a Hibenate Request, my Sql Request is like this :
Select city , count(*) from user group by city;
Thanks !
Use the code snippet
session.createSQLQuery("Select city , count(*) from user group by city");
The Hibernate can be used to write raw SQL code in the query.
EDIT:
As suggested hibernate also have it's own query language HQL which allows to create Query. And it uses more user friendly interface to query objects instead of tables. In your case the query would rewrite like
session.createQuery("Select user.city, count(*) from User as user group by user.city");
not a big difference but the object User is used instead of table, and its attributes.
With the criteria API the same could be achieved using projections in
session.createCriteria(User.class)
.setProjection(Projections.projectionList()
.add(Projections.rowCount())
.add(Projections.groupProperty("city"))
);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Why do we require native SQL in hibernate when we have HQL
can anyone explain with example
There are different dialects for each database. If you use native sql you can query using the dialect of a specific database. Sometime this is not possible using hql.
As an example you can use JSONB type in postgres that is a data type storing jsons. You can create queries accessing the content of this jsonb field that is not accessible with standard hql.
As an example the following query:
SELECT info -> 'customer' AS customer FROM orders;
select the property customer in the json stored in the field info. This has not equivalent in hql.
HQL is internally converted to SQL. SQL is the standard way to communicate with SQL Database.
HQL was created to help Java developers in writing SQL. Hibernate internally take cares of this conversion.
And each database has some specific functions which can not be used with HQL (for example Postgres database uses some extensions like PostGIS which has specific methods) and some complex queries are also very tough to write with HQL.
Writing below query with HQL is almost impossible (although with some external JARs that can done in workaround way, but not as simple as this one)
SELECT ST_AsText(ST_TRANSFORM(ST_GeomFromText(:geometry), :transformation, :toSrid))
HQL implements just a small subset of SQL. This small subset is very useful for CRUD-type operations, but little more than that.
SQL on the other hand has a full feature set of strategies and operators that are of no interest to HQL. When a query requires a more-than-trivial solution, the typical workaround is to avoid HQL and use "native SQL" instead.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to insert this values to a new table but eclipse doesn't read this query:
INSERT INTO my-list SELECT * FROM cars WHERE color='RED'
Use executeUpdate() Or execute() to issue data manipulation statements. executeQuery() is only meant for SELECT queries.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am working on a batch application where I have to retreive millions of records and need toprocess it individually. We are using hibernate to connect to DB.
I have tried using HQL and Criteria API to fetch the records(using the query Select * from table_name).
But it is taking more than 5 min .Sometimes it is getting hanged.
Can anyone suggest me the best approach for the data retrieving.
You may want to employ cursors, and if your queries are as simple as you indicated, pure JDBC may be sufficient - like here: https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html
Also, consider adding the WHERE clause to eliminate rows that can be recognized as irrelevant for your purposes.
And depending on your table and queries, adding an index may also help.
If you need to use hibernate, you can use ScrollableResults as mentioned here: https://stackoverflow.com/a/9891008/455449
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I would like to be able to query workflow tasks assigned to a list of users using Activiti. For instance, query tasks assigned to user1 or user2.
I have read the code of WorkflowServiceImpl.java and ActivitiWorkflowEnginge.java, actually WorkflowTaskQuery is able to query tasks assigned to only 1 user.
I can't find how to create a new method that adds the feature I need: I did not understand how the query is converted and what exactly I can modify.
I thought if it is possible to use luceneSearch for such query.
Thanks,
If for list of users you mean a group you could use the method with signature
List<WorkflowTask> getPooledTasks(String authority) in this way:
getWorkflowService().getPooledTasks(groupName);
In general you can use a method like:
List<WorkflowTask> queryTasks(WorkflowTaskQuery query,
boolean sameSession)
and then something like:
WorkflowTaskQuery query = new WorkflowTaskQuery();
query.setActive(true);
query.setTaskState(WorkflowTaskState.IN_PROGRESS);
query.setActorId("username");
List<WorkflowTask> results = workflowService.queryTasks(query, false);
where username represent the user you're looking for.
You can easily iterate over your user list to achieve what you need, but if this list is quite long you may encounter performance issues.
You can consider creating a temporary repository group, then add it all the user of your list and finally perform a pooled query against this group.
Remember that the WorkflowTaskQuery object is nothing but a filter you can compose for the queryTasks method.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
i am creating a website called hospitalService and i have a database for this with tables doctor_details and patient_details now i want a java code to generate automatic id which looks like d001 for doctor and p001 for patient please help me to write this code thanks in advance
You can use sequence generator to generate your unique Id. For each category you said above(eg: doctor, patient) you can create different sequence. The method and syntax to create a sequence depends on the DB that you are using.
Furthermore, if you are using JPA or Hibernate or any other persistence API, then most probably you will be enable to generate such sequence with the help of API only.
For ex: In JPA you can use the following annotation to create a sequence(Note that there are other ways to do the same but this is just for your reference).
#GeneratedValue(generator="my_seq")
#SequenceGenerator(name="my_seq",sequenceName="MY_SEQ", allocationSize=1)
Always depend on the respective DB provided sequence for populating primary id of each record in DB.
Please check the respective DB documentation for usage of Sequence type DB objects.
For oracle refer here: http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_6015.htm#SQLRF54244
While applying DB insert take next value of sequence and apply your custom desired string e.g. for doctor : "d"+ next value of doctor sequence or for patient: "p" + next value of patient sequence.