Reputation: 329
I am trying to connect to sql server using by Java application. While connecting to H2 database, I appended schema=schema_name
to the connection string in my java application and that worked.
However the same does not works for sqlserver.Connection is established but if my query does not contains the schema name prior to table name then query execution fails.
Please let me know of JDBC supports schema name in connection string or their is any other way.
Upvotes: 2
Views: 9075
Reputation: 2870
Sure, It is possible if we are using hibernate
or spring datasource ORM
,
let’s break down the CustomPhysicalNamingStrategy
class you’ve provided. This class is part of Hibernate’s API, specifically used for customizing how Hibernate maps entity names and database table names between Java and SQL.
CustomPhysicalNamingStrategy
The CustomPhysicalNamingStrategy
class implements the PhysicalNamingStrategy
interface from Hibernate. This interface allows you to define custom naming strategies for various database elements like tables, columns, sequences, and catalogs.
Here’s a detailed explanation of each part of the class:
public class CustomPhysicalNamingStrategy implements PhysicalNamingStrategy {
CustomPhysicalNamingStrategy
: This is the name of your custom naming strategy class.implements PhysicalNamingStrategy
: This means that your class must provide implementations for all the methods defined in the PhysicalNamingStrategy
interface.private static final String SCHEMA = "job";
SCHEMA
: A constant used to define the schema prefix that will be added to table and sequence names.toPhysicalCatalogName
@Override
public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment context) {
return name;
}
toPhysicalSchemaName
@Override
public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment context) {
return name;
}
toPhysicalCatalogName
, this method converts the schema name from logical to physical form.toPhysicalTableName
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return new Identifier(SCHEMA + "." + name.getText(), name.isQuoted());
}
SCHEMA
) to the table name. For example, if the logical table name is Employee
, it will be converted to job.Employee
.name.getText()
retrieves the text of the logical table name, and name.isQuoted()
maintains whether the original name was quoted.toPhysicalSequenceName
@Override
public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment context) {
return new Identifier(SCHEMA + "." + name.getText(), name.isQuoted());
}
toPhysicalTableName
, but it deals with sequence names.toPhysicalColumnName
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
return name;
}
The CustomPhysicalNamingStrategy
class customizes the physical naming of tables and sequences in the database by adding a schema prefix (job.
) to their names. It leaves catalog names, schema names, and column names unchanged. This can be useful for ensuring that tables and sequences are organized under a specific schema in the database.
Upvotes: 0
Reputation: 1323
Sadly, that is not possible. What you might want to do is set default schema for that user instead.
create login foo with password = "superstronkpassword";
create user foober for login foo with default_schema = "my super schema";
Upvotes: 2