I am trying to get the name of the database I am connected to in SQL Server. I tried doing:
Query query = session.createQuery("SELECT db_name()");
List<String> dbNames = query.list();
However, I got the following error:
[ERROR PARSER:35] *** ERROR: <AST>:0:0: unexpected end of subtree
Exception in thread "main" java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.MethodNode
\-[METHOD_CALL] MethodNode: '('
+-[METHOD_NAME] IdentNode: 'db_name' {originalText=db_name}
\-[EXPR_LIST] SqlNode: 'exprList'
How can I get the name of the database I am connected to?
-
AFAIK you cannot call NAtive database functions that way. Try using a Native Query instead of a simple query : http://www.roseindia.net/hibernate/hibernate-native-sql.shtml
-
You can either:
Create a native SQL query, with
session.createSQLQuery(...). You can extract a single row of results withuniqueResult().Obtain a JDBC
Connectionfrom theSession, and extract the connection string from the database meta-data. For SQL Server, I believe you'll need to parseconnection.getMetaData().getURL()in order to extract the actual database name.
Note that
Session.connection()is considered deprecated, and you're supposed to useSession.doWork().Ascalonian : Thank you! I didn't even think about using the Connection from the Session object.
0 comments:
Post a Comment