A trigger is most definitely not the only way.
In my experience, triggers can be unpredictable and performance hindering tools. If you really want to stream changes from one database to another, I might suggest that you check out Debezium for doing that task because it can do it asynchronously without impacting your run-time performance.
That’s because that isn’t how Hibernate works.
Traditionally, if you need to operate on multiple databases, you’ll need to have a
SessionFactory that is configured per database, acquire a
Session from each and perform your operations. But in this scenario, you need to take special care with transaction management, making sure the distributed transaction is committed correctly to guarantee database consistency across those participating in the transaction.
I believe I mentioned earlier, some databases have a way to mirror a remote table as a real table. This means you delegate the transaction management aspect to the database and you simply configure and interact with a single
SessionFactory. If your platform supports this, this is the best alternative.
The Debezium solution avoids the distributed transaction concerns and it also handles replication in an asynchronous way, allowing the run-time overhead to be non-existent. It will mean the user would have to use a
SessionFactory for their main database reads/writes and a seperate
SessionFactory for reading from the audit datastore.