Hibernate NonUniqueObjectException - java

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.

Related

Insert into Select using JPA

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.

JPA - One to Many Mapping using Native Query

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?

Update all table data at a time in JPA

Is it possible to update all table data in one query?
I have a Database table Person and corresponding Entiry PersonEntity, I can get all Person data vi a JPA in a list such as List personAll.
I have several CRUD operation on personAll instance, I want to reflect all these changes to the Database in one hand using Hibernate JPA
In other words I want content of Person Table is replaced with new content of personAll instance?
Actually long solution way of this question is execute several insert, delete and update operations. But there should be a easy way of doing it?
I can do similar thing when there are two tables Shool Student table if there is OneToMany relation between eash other? Hibernate JPA value removing OneToMany relation
Thanks
It depends on how many rows there are in your table.
If you load all the rows into a Hibernate session and modify the returned instances as required, any changes will be automatically persisted to the database by Hibernate when the session is flushed.
The reason it depends on the size is that if you load the contents of a huge table into a Hibernate session you risk out of memory errors and even if you don't run out of memory the flush will be very slow since every entity in the session much be checked for modifications.

Inserting data in one table using HQL in Hibernate

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.

Hibernate bulk update leads to in-query which takes for ever to complete

Article entity is a sub-class of the Product entity. The inheritance strategy for them is joined. Article#flag is a boolean attribute which I want to set false for all articles. Hence, I do
Query query = entityManager.createQuery("update Article set flag=:flagValue");
query.setParameter("flagValue", false);
query.executeUpdate();
I expected this to lead to a single SQL statement against the database which should complete fairly quickly. Instead Hibernate populates a temporary table (which does not physically exist in the database) and runs an in-query ie. the update later:
insert into HT_article select article0_.id as id from schema.article article0_ inner join schema.product article0_1_ on article0_.id=article0_1_.id
update schema.article set flag=0 where (id) IN (select id from HT_article)
The actual update statement takes "forever" to complete and locks the affected articles thereby causing lock exceptions in other transactions. By forever I mean more than an hour for 130000 articles.
What's the explanation for this behavior and how could I solve it? Other than running a native query I mean...
Update 2011-05-12: it's terribly slow because the in-query is slow, I filed a bug for that -> http://opensource.atlassian.com/projects/hibernate/browse/HHH-5905
I am using InheritanceType.JOINED of hibernate and faced a similar issue where hibernate was inserting into the temp table and was taking ages to delete the entities. I was using createQuery and executeUpdate to delete the records which caused the issue.
Started using session.delete(entity) which solved the issue for me.
Because you're using InheritanceType.JOINED, Hibernate has no choice but to unify the subclass and its base class.
If you switched to InheritanceType.TABLE_PER_CLASS (http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/jpa_overview_mapping_inher.html) you'd avoid this and get your performance back. But I'm sure you're using JOINED for a reason :-(

Categories