DataDirect Code Sample
How to Configure DataDirect with Hibernate
Document Number: 2471039PF
Category: Code Sample
Created: 09/09/2005
Last Modified: 09/09/2005
Product Information
Product: Connect for JDBC
Version: 3.5.00
Operating System:
Database: Document Details
Hibernate How-To's
The following describes the steps one must take when configuring DataDirect drivers with Hibernate. Two choices are available that fit directly with the JDBC API. The java.sql.Connection and java.sql.DriverManager class can be used, or the JDBC DataSource is also available.
Hibernate How-To’s - DriverManager
In this example, we'll configure Hibernate to work with Microsoft SQLServer.
Edit $HIBERNATE_HOME/etc/hibernate.properties
Add the following lines to configure DataDirect SQL Server JDBC driver
hibernate.connection.driver_class = com.ddtek.jdbc.sqlserver.SQLServerDriver
hibernate.connection.url = jdbc:datadirect:sqlserver://server_name:1433
hibernate.connection.username = sa
hibernate.connection.password = sa
Next, configure the correct dialect
hibernate.dialect org.hibernate.dialect.SQLServerDialect
Start a new session to the data source
SessionFactory sessions = cfg.buildSessionFactory();
Session sesssion = sessions.openSession();
Hibernate How-To’s – DataSource
DataSource instances are more likely to be encountered in Java EE environments. The is the class name we need to use to configure a DataSource in Hibernate with DataDirect’s SQL Server driver.
com.ddtek.jdbcx.sqlserver.SQLServerDataSource
Next we configure JNDI name
hibernate.connection.datasource = java:/comp/env/jdbc/sqlsvr
hibernate.connection.username = sa
hibernate.connection.password = sa
And set the appropriate dialect
hibernate.dialect org.hibernate.dialect.SQLServerDialect
The following code, shows how you obtain a connection from the context in an application
javax.naming.Context ctx = new javax.naming.InitialContext();javax.sql.DataSource ds = (DataSource)ctx.lookup("java:/comp/env/jdbc/sqlsvr
");
Connection con = ds.getConnection();
Hibernate – Gotchas
- Possible pitfalls and what to look for in a quality JDBC driver for Hibernate: Stability, Performance, Proven track record
- Dialect Issues - report to the JDBC driver vendors and to Hibernate
- Most difficulties can be avoided using the JBoss Certification program
DISCLAIMER: The sample code is not supported under any DataDirect Technologies support program or service. The sample code is provided on an “AS IS” basis. DataDirect Technologies makes no warranties, express or implied, and disclaims all implied warranties including, without limitation, the implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample code is borne by the user. In no event shall DataDirect Technologies, its employees, or anyone else involved in the creation, production, or delivery of the code be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample code, even if DataDirect Technologies has been advised of the possibility of such damages.