I have to map a large enterprise database schema. As a number of legacy applications are depending on that schema I can't change it from composite keys to surrogate keys (other than using views with instead-of triggers for ~70% of database objects) which led me to a number of issues I don't know how to overcome.
- How to map tables that have a surrogate (primary, auto-increment) key and a composite unique key to which a number of tables form a many-to-one relation over the surrogate key, and at the same time a number of legacy tables relate over the composite unique?
- What to do when several relationships over composite keys share the same component (Naselja: Korisnik/Opcina, Korisnik/Grad)? Hibernate tells me I can't use the same column (Korisnik) twice, unless I add 'updatable = false, insertable = false' to the @JoinColumn annotation. Yet when I do that, I get the message that all components of the relation have to have the same settings for insertable and updatable. How do I ever update or insert the second relation then?
- Inserting a row into 'Ulice' table will give me a error "org.hibernate.AnnotationException: referencedColumnNames(Korisnik, Naselje) of lc.data.hibernate.Ulice.naselja referencing lc.data.hibernate.Naselja not mapped to a single property" unless in 'Naselja' table I explicitly add a many-to-one relation to 'Korisnici' table in the model, which doesn't reflect the actual database schema.
From what I've read so far JPA doesn't support relationships to non-primary keys, but people still do it. Also my second problem might be solved with Hibernate 5, but I'm hoping for some good advice what route to take here.
Test model, database scripts and projects: