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.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Problem - How do I create a table and insert data based on a query that is generated in Java?
Background - There are a series of tables the queries are based on (20 or so) but there are common operations, like left joining on FKs to the same tables and where clauses that are identical. I'm looking for a clean way to create the 20 queries without rewriting the same joins 20 times.
What I've done so far -
I've built a small application which executes a moderately complex query (sub queries, unions, left joins) and inserts the results of the query into a new table using ;
Select col1 as new_col1, col2 as newcol_2
into new_table
from ( .... )
I've done this by writing a base SQL file which contains place holders for the column names and new_table which I then replace using a simple string replace in my Java code. I've created about 20 different base SQL files because the from ( .... ) section references different tables and it's a bit too complex to build that part of the query without some libraries.
An easy way to wrap Java objects around a DB is to create entity classes and controllers. Netbeans has great tools to help you generate entity's and controller classes. Documentation here.
File->new File
then search for entity class from database, follow steps in wizard.
After class are made you will want to make JPA controllers
File-> new File
then search for JPA controller form entity class.
Select all the entity class and generate! Once you do that I will show you how to use said entity's and controls!
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 have a couple of years of programming experience in Java (Android) and this is my first time with the use of API's to retrieve JSON data. So to get basic facts on a queried topic I thought of using Freebase API however this is now going to be deprecated and Google is moving it to Wikidata. However the query API is still in beta and I simply cannot understand the query API documentation or how to retrieve the facts. So is there an alternative to Wikidata and Freebase?
These are my final questions:
Can someone please explain to me how I would go about using the Wikidata query API? AND retrieve the facts in a readable format?
Or can someone suggest me a better alternative to Freebase other than Wikidata?
The data you want to access is stored in a Subject Property(also Predicate) Object (SPO) format. That means you have a subject and an object that is associated with a property for example <Albert_Einstein> <wasBornIn> <Germany>.
Usually you can access those SPO-databases over an endpoint using SPARQL. SPARQL is a SQL like language which allows you to formulate queries to access the data. Fortunatly for you Wikidata also has a sparql endpoint you can use: https://query.wikidata.org/
Here is a simple example which will load all subjects that are referenced, using a rdf-schema label, to the String "Titanic" and limit the results to 100 entries.
select distinct ?a where {?a <http://www.w3.org/2000/01/rdf-schema#label> "Titanic"#en } LIMIT 100
To query Wikidata in Java you can use Jena which will allow you to use SPARQL-queries and the endpoint to access the data.
As far as I know you can also access Wikidata using http but there a few benefits using SPARQL. There are two other big databases I know of that you can use and both of them have a SPARQL endpoint. So it's quite easy to change the endpoint to access the other two databases. It's also possible that one database contains a reference to another database which you can follow to gather more data.
Since you also asked for alternatives the two databases I mentiond are
DBpedia (SPARQL-Endpoint) and Yago (SPARQL-endpoint). Both use Wikipedia to extract facts and therefor they are huge. Yago also uses WordNet to build a nice taxonomy you can use to classify your data. DBpedia on the other hand has a lot of references to other sites you can use.
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 have a problem with performance of my program.I have a java program that connect to MySql database and do some process witch depend execute SELECT query on database.
Now problem is that my program for process have to execute 130,000 select query on mySql and this need a long of time.
I have 10 minutes for do all process.
Have any idea how do execute 130000 select query maximum in 10 minutes?
Have any idea how do execute 130000 select query maximum in 10 minutes?
It's a 200 queries per second, this should be doable for simple queries. However, the simplest solution is probably to replace them by a single query loading all needed data (you're already assuming that it fits in memory) and process it in Java.
As a HashMap is orders of magnitude faster than any database, the task will become trivial and your machine gets bored.
One Approach is to use InMemory databases Like H2 or HSQL. This Solution will work only if you can pre-load data into memory. Lot of other factors also need to consider like Volume of Data , Frequency of Changes in data in the database etc.
If this is possible, then you can query directly in Memory, which is always faster.
Identify the important data to be loaded to Memory
Create corresponding table structure in Memory DB like H2 Or HSQL
Load those data from your actual MySQL and Insert this in Memory DB
Fire Query in that DB
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"))
);
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
My Java application is using JPA for object persistence. The business domain is very simple (just three classes are persistent, with 3-5 properties in each). Queries are simple as well. The question is which approach I should use: JPQL or Criteria API?
I'm pretty sure this has already been covered here on SO but I couldn't find the existing question. So, here is my point of view on the question:
I find JPQL queries easier to write/read.
I find the Criteria API nice for building dynamic queries.
Which is basically what you'll find in Hibernate: Criteria vs. HQL.
But there is one major difference between the JPA 2.0 Criteria API and the Hibernate's Criteria API that is worth mentioning: the JPA 2.0 Criteria API is a typesafe API and thus gives compile time checks, code completion, better refactoring support, etc.
However, I don't find that the benefits outweighs the ease of use of JPQL.
To sum up, I would favor JPQL, except for dynamic queries (e.g. for multi criteria search features).
Related questions
Hibernate: Criteria vs. HQL
What are some of the real world example where JPA2 Criteria API is more preferable?
More resources
Hibernate Querying 102 : Criteria API
I answered a similar question previously and I will re-post my answer here for the benefit of the community. I'm going to assume you are using an Application Server vis-a-vis my answer below.
The Criteria API exists to allow for the construction of dynamic SQL queries in a type-safe manner that prevents SQL injection. Otherwise you would be concatenating SQL strings together which is both error prone and a security risk: i.e. SQL Injection. That would be the only time you would want to use the Criteria API.
If the query remains basically the same but need only accept different parameters you should use annotated #NamedQueries which are simpler, precompiled, can be cached within the secondary cache and possibly validated during server startup.
That's basically the the rule of thumb concerning Criteria Queries versus #NamedQueries. In my experience rarely do you require the Criteria API but it is good that it exists for those rare times it is required.
Hope this helps.