I'm trying to run an example where Many events can be held at one single location (Many to One association), which I could run successfully, if this is a unidirectional from Event to Location. But if it needs to be bidirectional, that I should be able to retrieve events for a given location, I'm failing. Please see the below code.
My client application
I'm using MySQL 5.1 as the backend database and have mentioned the generator as "Native"
1) In this bidirectional relationship from Event to Location, where in Event has a reference of Location and Location has the collection of events, which one should be persisted first. Either way, I'm getting exception
That is true. I initially started this application as Many to One (Many events at one location), but later gave a thought of getting all events at one location. But how do I persist these objects in database. Which one should be persisted first. Or , should I be using a third table/object to store the primary keys and break down the Many to Many mapping to two Many to One mapping.
Joined: Jul 20, 2009
You can break the many-to-many into two one-to-manys (*my* preferred way, esp. if you want to add columns to that relation) or you can map it in normal hibernate style. You can find good samples in the hibernate reference docs.