Sleep mode

I have an object mapped to One-To-One according to the following code:

@Entity @Table(name = "my_entity") public class MyEntity { ... @OneToOne @JoinColumn(name = "site_id") private Site site; ... } 

I was just told that I should start storing MyEntity records with a value for 'site_id' that might not exist in the site table. I still need to save the value for "site_id", however it will not match the essence of the site.

The only thing I can think of is to create a 2nd Entity type mapped to the same table that does not display a one-to-one / join in the site table.

Is there a way to do this without creating a second mapped object for the same table?

thanks paul.

+1
hibernate one-to-one
source share
1 answer

This is a very bad design. The foreign key must be a foreign key and must not point to non-existent strings. I would correct the data / design.

If you really can't, use the NotFound annotation described here :

By default, when Hibernate cannot resolve the association because the expected related item is not in the database (invalid identifier in the association column), an exception is thrown in Hibernate. This may be inconvenient for legacy and poorly supported circuits. You can ask Hibernate to ignore such elements rather than throw an exception using the @NotFound annotation. This annotation can be used in association with @OneToOne (with FK), @ManyToOne, @OneToMany, or @ManyToMany.

+3
source share

All Articles