I've been trying to get a list of values from database using hibernate.
I'm using MS SQL Server XE 2005 and using Hibernate 3.2.2 to fetch DB rows. I've tried JDBC drivers provided by Microsoft as well as jTDS but all I get is an empty list.

Here is my hibernate configuration file.

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
    
    <session-factory>
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433;databseName=SQLTutorial;integratedSecurity=true;</property>
        <!--property name="hibernate.connection.username"></property>
        <property name="hibernate.connection.password"></property-->
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="show_sql">true</property>
        <property name="transaction.factory_class">
            org.hibernate.transaction.JDBCTransactionFactory
        </property>
        <property name="hibernate.cache.provider_class">
            org.hibernate.cache.HashtableCacheProvider
        </property>
        <property name="current_session_context_class">thread</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <mapping resource="hibernate\tutorials\Department.hbm.xml"/>
        
    </session-factory>
    
</hibernate-configuration>

Am I missing something here?
And my hbm mappings are

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
        
<hibernate-mapping package="hibernate.tutorials">
    <class name="Department" table="Department">
        <id name="id" column="DEPT_ID" type="long">
            <generator class="native"/>            
        </id>
        <property name="departmentName" column="DEPT_NAME"/>
    </class>  
    <class name="Employee" table="EMPLOYEE">
        <id name="id" column="EMP_ID">
            <generator class="native"/>
        </id>
        <property name="firstName" column="FIRST_NAME"/>
        <property name="lastName" column="LAST_NAME"/>
        <!--one-to-one name="department" class="Department"/-->
    </class>
</hibernate-mapping>

The output that I always get is

Hibernate: select this_.EMP_ID as EMP1_1_0_, this_.FIRST_NAME as FIRST2_1_0_, this_.LAST_NAME as LAST3_1_0_ from EMPLOYEE this_

Also, I have successfully retrieved the result sets using plain JDBC

Seems there's a communication gap between Hibernate and SQL Server Express Edition (2005/2008).

I tried to use Hibernate with SQL Server 2005 and 2008 Express Edition but in vain.

Finally, gave up and used MySQL. It gives me the expected result.

hbm mappings for MS SQL server require additional properties which I just figured out with the help of Netbeans.
Thanks to Netbeans

The additional properties are schema and catalog

I modified the mappings as shown below and it gave me the expected results

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
        
<hibernate-mapping package="hibernate.tutorials">
    <class name="Department" table="Department" schema="dbo" catalog="SQLTutorial">
        <id name="id" column="DEPT_ID" type="long">
            <generator class="native"/>            
        </id>
        <property name="departmentName" column="DEPT_NAME"/>
    </class>  
    <class name="Employee" table="EMPLOYEE" schema="dbo" catalog="SQLTutorial">
        <id name="id" column="EMP_ID">
            <generator class="native"/>
        </id>
        <property name="firstName" column="FIRST_NAME"/>
        <property name="lastName" column="LAST_NAME"/>
        <many-to-one name="department">
        	<column name="DEPT_ID" />
        </many-to-one>
    </class>
</hibernate-mapping>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.