简体   繁体   中英

passing schema name as parameter in native query

I have the below two queries which run on different schema altogether below are the queries:

select abccode from abc_first.table_configurations_name;

select abccode from abc_sec.table_configurations_name;

for the first query the schema is abc_first and the for the second query the schema is abc_sec now I have to write the parameterized query in JPA whereas a parameter I will be getting the schema name and as per that schema name I will be executing the query prefixing the schema name something like that

select abccode from {schema-name}.tablename ;

please advise how can this be achieved with spring boot JPA.

You can use the entity manager to create a query like this:

...

@PersistenceContext
private EntityManager entityManager;

public someMethod(String databaseName, String schemaName) {
    this.entityManager.createNativeQuery("select abccode from" + databaseName + "." + schemaName + ".tablename");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM