Want to set up Hibernate with SQL Server, no problem, install the Microsoft JDBC driver, get the connection details, set up the hibernate config file and you’re good to go. If only it was that easy.
What should have been a simple task ended up taking a lot longer than I would have thought. With that in mind, a quick blog post detailing the steps I took should save others time and annoyance in setting up Hibernate
Firstly install the Microsoft JDBC driver. Instructions are provided by Microsoft on setting this up. You need this to be able to connect to SQL Server
Secondly, install the Hibernate plugin in Eclipse
From the Eclipse IDE menu, select Help >> Install New Software … and enter the following URL “https://download.jboss.org/jbosstools/updates/stable”. Filter by Hibernate and install the Hibernate Tools
Once Eclipse has restarted, you can change the perspective to Hibernate from Window >> Open Perspective >> Other
From the Hibernate section, click the + icon to add a new configuration.
In the popup
Select Hibernate Version: 4.0
Select your project
Select Hibernate configured Connection for the database connection
Click Setup for the configuration file and select Create new…
Give the config file a name and then fill out the details for the config file
Select SQL Server for the dialect
Select the Microsoft JDCB driver that you installed earlier
Enter dbo for default Schema
Add in a connection string, username and password for the database and finally click Finish
This should create a [filename].cfg.xml file.
Click on the Source tab.
Now you need to make a couple of changes to the properties
<property name=”hibernate.connection.driver_class”> com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name=”hibernate.connection.driver_class”> com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
Notice the difference?
Without those little changes, Hibernate will not be able to connect to the database. Now set up your session factory and you should be good to go.