I don't think that Hibernate can take care of all situations especially when dealing with something like Transact-SQL or CREATE GLOBAL TEMPORARY TABLE or even CREATE TEMPORARY TABLESPACE and then you have the AS, AS SELECT, and even PARALLEL COMPRESS AS SELECT after the table name to consider.
As an alternative however you can create a method which can retrieve the Table Name from a supplied CREATE TABLE SQL string which I believe will cover most (if not all) of these issues. Below is such a method:
public String getTableNameFromCreate(final String sqlString) {
// Always rememeber...we're only trying to get the table
// name from the SQL String. We really don't care anything
// about the rest of the SQL string.
String tableName;
String wrkStrg = sqlString.replace("[", "").replace("]", "").trim();
// Is "CREATE TABLE" only
if (wrkStrg.startsWith("CREATE TABLE ")) {
wrkStrg = wrkStrg .substring(13).trim();
}
else if (wrkStrg.startsWith("CREATE GLOBAL TEMPORARY TABLE ")) {
wrkStrg = wrkStrg .substring(30).trim();
}
else if (wrkStrg.startsWith("CREATE TEMPORARY TABLESPACE ")) {
wrkStrg = wrkStrg .substring(28).trim();
}
// Is it Create Table ... AS, AS SELECT, PARALLEL COMPRESS AS,
// or PARALLEL COMPRESS AS SELECT?
if (wrkStrg.toUpperCase().contains(" PARALLEL COMPRESS ")) {
wrkStrg = wrkStrg.replace(" parallel compress ", " PARALLEL COMPRESS ");
tableName = wrkStrg.substring(0, wrkStrg.indexOf(" PARALLEL COMPRESS ")).trim();
}
else if (wrkStrg.toUpperCase().contains(" AS ")) {
wrkStrg = wrkStrg.replace(" as ", " AS ");
tableName = wrkStrg.substring(0, wrkStrg.indexOf(" AS ")).trim();
}
// Nope...none of that in the SQL String.
else {
tableName = wrkStrg.substring(0, wrkStrg.indexOf("(")).trim();
}
// return but remove quotes first if any...
return tableName.replace("\"","").replace("'", "");
}
If the database name is attached to the table name as in your example (test_database.test_table) then of course you will need to further parse off the actual table name.