Home > database >  Hibernate detached session
Hibernate detached session

Time:07-30

I'm trying to regenerate relation table user_product, from other tables( a lots of INNER JOINS from user --> tableX --> tableY --> tableZ --> product)

I delete the content of user_product and then inserting new values:

INSERT INTO user_product (column1, c2, c3) 
SELECT userId FROM users INNER JOIN...etc

However in finding the new relations I need to use previous data from user_product. Specifically I need do something like (SELECT up.id from user_product WHERE up.id = u.id) to filter some values.

Is there an elegant way to do it in hibernate? For example detaching the whole session and and deleting user_product there while stil being able to access original data?

CodePudding user response:

There is not really a way to do this, because as soon as you run the insert statement, all the pending removal operations will be flushed first. Even if Hibernate wouldn't do that (you could set an empty query spaces set on a native query), wouldn't the insert the run into constraint violation errors because the records are still there?

Might be better to run whatever kind of migration you are trying to do in a way that doesn't delete rows, but maybe renames the table (to e.g. xxx_old) and creates a new empty table, so that you can migrate rows you are interested in from one table to another.

  • Related