Search code examples
javahibernatehibernate-annotations

Hibernate Many-To-One using other schema on DB2


I am using hibernate-annotation (entitymanager) to handle my db2 databases using separate schema.

My main schema is called "mainschema". It has a table for fileuploads.

Then I have some other schema (schema1, schema2, schema3, schemaN).

How can I tell the many-to-one relationship on the schemaN to refer to the filetable on "mainschema". If I open the connection, I tell which schema to use. But the many-to-one is still using that schema and the fileupload table is only available in the "mainschema"

Thanks for help!

EDIT:

My fileupload hibernate bean using explicit the main schema:

@Entity
@Table(name="DOKUMENT", schema="mainschema")

Then I have one schema for every client, have a look at here: The Schema is not set in the bean. It is set instead in time where the connection is opened.

@Entity
@Table(name="SOMETABLE")

This table "SOMETABLE" is existing in every schema for every client. It refers to the dokument entity with an many-to-one

@Many-To-One
@JoinColumn(name="DOKUMENT_ID")
public Dokument getDokument() { return dokument }
public void setDokument() { this.dokument = dokument }

Question can be closed. It is working without any changes, because hibernate is still using "mainschema" automaticly.


Solution

  • This is how to map SchemaN to MainSchema one-to-many

    @Entity
    public class SchemaN{
    
        @OneToMany(targetEntity=MainSchema.class, mappedBy="pk_SchemaN")
        private List<MainSchema> ms;
    
    ...
    
    }
    
    @Entity
    public class MainSchema{
    
         @ManyToOne
         @JoinColumn(name="FK_MainSchema")
         private SchemaN pk_schemaN;
    
    ...
    
    }
    

    Hope this helps you it should be the same for any Schema1 to N