MCLonips
MCLonips

Reputation: 99

Spring: map entity to a scheme

Im trying to create a Spring-Boot Data JPA-Application with Entities based in multiple schemes using an oracle Database.s I have two Schemes, scheme_a and scheme_b. The DDL User for scheme_a is scheme_a, the DDL user for scheme_b is scheme_b. Both DDL user will be used by liquibase to create my initial table structure.

My application has two entities:

My application has a user app_user, with CRUD-rights on all tables and sequences located in scheme_a and scheme_b. This user is my DMA-user

My plan was to use Spring-Data-Jpa to connect with the database using the app-user user. This user should be able to work with all entities in scheme_a and scheme_b.

Example of Entity foo (bar has identical structure):

@Entity
@Table(name = "T_FOO")
public class FooEntity {

public FooEntity () { // no-args c-tor for hibernate
    }

    @Id
    @Column(name = "foo_id")
    @SequenceGenerator(name = "sequence_foo_id", sequenceName = "sequence_foo_id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence_foo_id")
    private long id;

    @Column(name = "foo_name")
    private String name;
}

Every entity has its own Repository:

public interface FooRepo extends CrudRepository<FooEntity, Long> {

...

}

My configuration:

@Configuration
@EnableJpaRepositories(basePackageClasses = {FooRepo.class, BARRepo.class})
@EntityScan(basePackageClasses = {FooEntity.class, BarEntity.class})
public class AppDBConfig {

...

}

But every time my application tries to start it is not able to locate the table T_FOO based in scheme_a.

I am not able to extend the @Table-Annotation with corresponding scheme. Does any one know a way to solve this problem? Is it possible to create something like a "scheme-table" to tell hibernate in which scheme which entity is located, like

var schemeMap = new HashMap<Class, String>();
schemeMap.put(FooEntity.class, scheme_a);
schemeMap.put(BarEntity.class, scheme_b);

Greetings from Germany!

Upvotes: 0

Views: 911

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81882

You need to specify the schema in the @Table annotation.

@Entity
@Table(name = "T_FOO", schema = "scheme_a")
public static void FooEntity {
// ..

Since you don't seem to be allowed to change the annotation on FooEntity I see a couple of options available to you:

If you can change the annotation on BarEntity but not on FooEntity you can make the schema of FooEntity your default schema and set the schema for BarEntity in the annotation.

If you can't change either entity but have some control over the database you can create a view or synonym in your main schema that mirrors T_FOO.

You can also configure Hibernate with XML and specify the schema there.

You could decompile the class file containing FooEntity, add the required annotation and compile it again.

You probably could use ByteBuddy or a similar tool to do that at runtime during startup of your application.

Upvotes: 1

Related Questions