2

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.

  1. 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?
  2. 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?
  3. 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:


Database schema MSSQL

MSSQL create database scripts

Java NetBeans example projects

4

1 回答 1

2

1) - 将自动递增列映射为主键 - 为复合 ID 创建类并使用 property-ref 映射多对一

    <component name="compIdProperty" unique="true">
      <property column="naselje" />
      <many-to-one column="korisnik" class="Korisnik"/>
    </component>

    <many-to-one name="Naselja" property-ref="compIdProperty">
      <column name="naselje" />
      <column name="korisnik" />
    </many-to-one>

2)AFAIK根本不可能使用hibernate

我看到的唯一可能的方法是将(Grad,Opcina)映射为属性并自己管理关联

3)由于这是一个遗留模式,我会妥协添加多对一并将多对一映射到 Opcine 和 Gradovi 中的 Korisnik

于 2012-05-14T12:24:49.083 回答