Business scenario:
I want to save data to another database schema when the data is deleted, I use listeners.
When I insert the data into another database, hibernate tells me that no associated entity exists.
For example, in the main database, the user id is 1 and the associated role id is also 1. When user 1 is deleted, saving user 1 to another empty database will report an error. How can I save only the user and not the role?
Well, it’s programming, so I guess you can make it work somehow, but I wouldn’t recommend you that, especially because the same entity object should not be attached to two different entity managers. There are some checks for this scenario, but you will see that when you try it.
I use one data source and another data source that only does insert operations to back up the data.
Can it be handled programmatically, I can’t get it working right now
I don’t know what else to tell you. You have one entity object and that can only be associated with one entity manager at a time. If you want to persist the same object with two entity managers you will have to flush and clear the first entity manager before persisting it in the other.
If you want to ignore some of the data in the second persist, you will have to alter the object or use some special orm.xml mapping for the second persistence unit.