GeekLondon.com Help icon Syndication Feed icon 

An EJB3 and ManyToOne question

One of the annotations in the EJB 3 specification that causes untold confusion is the mappedBy attribute of the association annotations. Mostly this is in the minds of people who think it's something to do with the cascade rules (it isn't).

I, on the other hand, have discovered a novel route to contra-enlightenment. My confusion is over the question of why it is not possible to make the ManyToOne entity of a bi-directional association not be the owner of the relationship.

With EJB3, this is enforced. If you have an @ManyToOne annotation, it simply doesn't have a mappedBy attribute, so you can't set it to the other entity's field. But why doesn't it?

You might reasonably point out that the entity on the ManyToOne end has to have the foreign key in its own table1. Ok. And? Why does that stop changes to the association from the other perspective from causing an update to the key in question? It works ok if you haven't got a ManyToOne association in the first place and update the OneToMany which (then) can own the association. What is it about being bi-directional that prevents this from being possible?

Am I missing something really forehead-slappingly obvious? Is it really impossible? Or is it just that it's technically difficult and so omitted from the spec for convenience? Or is it perfectly possible but arose from a limitation in one of the products that contributed to the standard? Or some other explanation that I can't even think of? I'd really like to know.

Since I've now written two books on Hibernate, which has the same limitation in both its XML based mapping format and the relatively new annotations based mappings, I'm actually kind of embarassed to have to ask this. But if you don't ask you don't find out.

Answers on a postcard to dave@paperstack.com please!

[Update] Amended several confusing typos.

1. This is not strictly true, but it's the closest thing to an explanation I can think of.
Posted at Oct 24, 2007 7:22:20 PM, and last updated Oct 24, 2007 7:27:04 PM