1

I'm working on a codebase where unidirectional JPA/Hibernate @OneToOne mapping is used on entities where actual database relationship cardinality between the entities is many-to-one.

However, it seems the code is working fine and it looks like there are no functional issues resulting from the use of unidirectional @OneToOne where unidirectional @ManyToOne should be used.

I would like to address this in the codebase but would need an integration test to demonstrate the problem.

Is there any testable case in which hibernate will fail when using unidirectional @OneToOne mapping where logically @ManyToOne should be used?

Krešimir Nesek
  • 5,302
  • 4
  • 29
  • 56
  • 1
    IMHO there is no difference between these two annotations when used unidirectional – Simon Martinelli Jul 23 '20 at 12:39
  • Have a look here https://stackoverflow.com/questions/21789769/jpa-unidirectional-onetoone-vs-manytoone-with-hibernate-no-difference?rq=1 – Simon Martinelli Jul 23 '20 at 12:41
  • I did, but I could not create any error / exception when creating a test in unidirectional scenario. Even if (in made up example) you assign multiple employees to same employer in same transaction, where employer is mapped with @OneToOne in employee entity, it still works "fine". – Krešimir Nesek Jul 23 '20 at 12:46
  • 1
    Yes because as I said there is no difference. – Simon Martinelli Jul 23 '20 at 12:47
  • You have some differences in the case of cascade deletions (as far as I can think) and if you want to delete orpahened rows. Otherwise as Simon says, it will be pretty much the same. – Augusto Jul 23 '20 at 14:28

0 Answers0