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.
Related
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?
I'm trying to create a new Play application using JPA / Hibernate from an existing Mysql database.
hibernate.hbm2ddl.auto in persistence file is set to "update".
My table posts has 6 entries.
Problem is when I execute an HQL statement like "SELECT p FROM Posts p", I got an empty result, while if I execute a native query like "SELECT * FROM posts" I got my existing entries.
I think I'm missing something huge here, like hibernate's internal caching system or something, is there anything like this that can explain this empty result ?
Does Hibernate has to build some specific cache when used from an existing database so that it returns correct results with HQL statements ? Why would it need such a thing ?
My table posts has 6 entries. Problem is when I execute an HQL statement like "SELECT p FROM Posts p", I got an empty result, while if I execute a native query like "SELECT * FROM posts" I got my existing entries.
This sounds like you have to define mappings from mysql tables to jpa entities. You can generate them automatically using hibernate tools (like see an example here on how to do that using an Eclipse plugin).
You should get some results from your query after that, Hibernate will populate its caches along the way.
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.