简体   繁体   中英

Updates multiple tables in hibernate and ensure data consistency

I am new to hibernate.Started exploring yesterday itself. I am stuck and wondering whether this could be done with Hibernate or not.

Lets say I have three tables Table A ( which has columns name, budget, bid, status , date ), Table B ( id, budget, bid ) and table C ( id, status ). I want all data to be consistent ie whenever the update happen in Table B (lets say for budget the new value should get reflected in Table A). Same applies whenever update happen in Table C also. What is the best way to achieve this in hibernate?

First of all, I don't recommend data duplication across multiple tables, which is very hard to support and maintain.

But as you are stating that it is your main requirement, you need to use JPA Entity Listeners (works with Hibernate plus across all ORM frameworks) postUpdate() and postInsert() so that whenever there is an update/insert to the Table B, then create & persist one more object (row in database) into Table A.

Below is the point taken from JPA spec. for your reference.

PostUpdate callback occur after the database update operations to entity data respectively.

You can find a simple example here

您应该在插入表B中的数据时使用触发触发事件,这将反映表A中的数据

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM