views:

35

answers:

1

Hi All,

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..

+1  A: 

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.

Pascal Thivent
Tnx for your answer, we actually want't to remove the contraint and kindof let go on referential integrity but just keep the relation so that we can map it again later in time and restore integrity.
Albert
@Albert: I see. I don't *think* Hibernate will crash as long as 1. the table is there 2. you do not load the `@One` part. I would test this to confirm this behavior (should be pretty easy). If it does crash, then mapping the FK as a String would be indeed a solution.
Pascal Thivent