简体   繁体   中英

Hibernate @ManyToOne on inconsistent database

Let's say I have two tables, employee and department where emp has a @ManyToOne key to dept. Now I want to be able to delete rows from the dept table but keep the emp records pointing to it; basically saving the relationship so that when the dept table is recreated the relationship is restored. (This actually happens in our system, but not with hibernate, but by using composed, reproducable keys).

The question is: Will hibernate crash on @ManyToOne relationships which seem to be there but with no record in the @One part of the relationship? (basically an inconsistent database state).

I probably should solve this by removing the @ManyToOne relationship and simply map the foreign key to a String or so. I just wondered whether we can pull it off to leave the relationship in place..

Now I want to be able to delete rows from the dept table but keep the emp records pointing to it;

Then you'll have to delete them logically, not physically (and FK constraints will actually prevent you from deleting departments).

Will hibernate crash on @ManyToOne relationships which seem to be there but with no record in the @One part of the relationship?

Don't you have referential integrity (see above)? But let's say you broke the integrity... Maybe you'll be able to load employees but something is going to crash at some point (when loading the association, if not before).

I probably should solve this by removing the @ManyToOne relationship and simply map the foreign key to a String or so. I just wondered whether we can pull it off to leave the relationship in place..

Delete the departments logically.

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