I was reading the Hibernate HQL tutorial and found that HQL doesn't support INSERT INTO..VALUES.. but INSERT INTO..SELECT.. i.e. HQL only support insert from another table.
Suppose I want to insert same values in one table and that data is not from any other table i.e. the values are not in any other table.Then how can I do that in HQL?
Also, would like to know the rational behind such restrictions in HQL?
You don't need to use hql to insert if the data is from another table.
Simply get a reference to your entity, get a hold of a Hibernate session, and call save().
According to http://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch04.html#d0e2116
Pseudo-syntax for INSERT statements
INSERT INTO EntityName properties_list select_statement
Only the INSERT INTO ... SELECT ... form is supported. You cannot specify explicit values to insert.
Hibernate is an ORM framework (Object-Relational Mapping).
Its job is that you give objects (Entities) to it and he manages the storage (through Session.save(), IIRC).
So, you do not use the HQL to insert new records, but use the ORM methods.
And (this is a guess) on the other hand, since loading entities from a table, copying them to other entities and storing them one by one is slow, HQL provides a shortcut to the SQL in the DB just for that specific operation for performance purposes.
You can use session.save(object) to insert data into tables.
Related
I am using JPA (with Hibernate) to talk to the database. I have a task of selecting all records from Table1 and insert into Table2. Can I achieve this using JPA in one step?
I tried using #Query annotation and provided a HQL query (INSERT INTO TABLE1.. SELECT * FROM TABLE2) to it but got a DML error.
Note: As a last resort I am doing it in two steps now, which seems to be inefficient:
step1: getAll from table1
step2: save to table2
JPA tries to do some O/R-Mapping for you.
Your request is just moving data between two relational tables (or a table and pseudo-table). So why do you need to use JPA to do that? Just go straight and use JDBC, or native query. And never forget that nothing is a silver bullet especially an O/RM.
With SQL I can copy data from one table to another mirror table. (e.g. insert into TABLE_EXAMPLE_COPY select * from TABLE_EXAMPLE; .
How can I do the same thing using Hibernate org.hibernate.Criteria or org.hibernate.Query or org.hibernate.SQLQuery?
If you want to perform that action from within the boundaries of JPA or Hibernate, the best way to accomplish that is to use a Native SQL statement.
session
.createNativeQuery( "INSERT INTO table_copy SELECT * FROM table" ).
.executeUpdate();
The other options involve reading the source table into a POJO and then transforming that into the POJO representation for the copy table and saving those rows. The problem with these is that you also introduce network latency and JVM overhead just to create an in-memory object, transform it, and then push it back over the network to the database.
The presented above solution avoids all those drawbacks and allows the database to handle all that in the best performing way it knows how.
I'm getting NonUniqueObjectException in hibernate.
There is one Item class, I saved list of Item objects using session.save of hibernate.
Now in the same transaction, I'm trying to update same Items using raw sql query which has join with another table. This gives me NonUniqueObjectException. The two tables I'm joining are unrelated as entities for hibernate, that is, there is no foreign key relation.
So I have 2 questions:
First, is there any way of using hql for writing inner join queries in hibernate.
Second, how to avoid NonUniqueObjectException.
One of the things that is working is that I clear the session before making any raw sql query. Any better approach is welcomed.
I've a SQL Query which returns one to many mapping data, for example I've JOB table along with multiple one to one dependent tables and a Warning Table with one-many mapping.
JOB 1 can have n number of warnings to it.
Right now we are handling it from Java logic but Is it possible to achieve it using JPA Native Query?
I see the same scenario's mentioned before How to map native sql results to oneToMany field with SqlResultSetMapping in JPA
This is the exact thing I'm looking for ? Will the Entity take care when used with native Query?
In my database code I use some Hibernate native SQL queries (inserts, deletion, updates). I understand that when I use HQL and the cache is on than the state of the DB is stable whenever I call the DB with the HQL. However, I wonder what happens if I use native SQL queries, e.g. I insert some data (but do not commit it) and than I try to fetch some data with a HQL query. Will I get the inserted data too?
Any hints?
I would say, that it depends on underlying database and transactions setting. But even HQL is translated to native SQL and executed. As long as you stay in same transaction, you will be able to load changes made with native SQL via HQL. But keep in mind, that HQL queries interat with caches, proxies and other hibernate stuff - there could be some weird issues, because natiev SQL completely bypasses this ( this is purpose of native queries - fast lane around all the hibernate stuff )
Adding to Konstantin Pribluda's answer I can say that in the reverse situation : adding data through Hibernate(even with session.save()) and then fetching data with native SQL resulted in the native SQL-query not fetching the added data.
So when using different types of query's in the DAO, I always flush the session first before I use a native SQL query... Never know which method's get mixed in the Service layer.
Yes, you will get it because all of your DAO queries will executed one by one whether its HQL or SQL. So if insert query is first in order then you will get inserted records.