Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8627893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:23:50+00:00 2026-06-12T08:23:50+00:00

I am trying to use Hibernate with SQL Server . I want to select

  • 0

I am trying to use Hibernate with SQL Server .

I want to select the rows from SQL Server and cast them to Places objects.

I will write the codes I tried and exceptions I got.

List places = session.createQuery("FROM PLACES").list();
for (Iterator iterator = places.iterator(); iterator.hasNext();){
   Place place = (Place) iterator.next(); 
}

Exception:

SEVERE: Incorrect syntax near the keyword 'from'.

This code generates

select  from  

and since SQL Server needs something between select and from this query fails

So I tried to change the code to

List places = session.createSQLQuery("SELECT ID,NAME FROM [PLACES].[dbo].[PLACES]").list();
   for (Iterator iterator = places.iterator(); iterator.hasNext();){
      Place place = (Place) iterator.next(); 
   }

Exception:

Exception in thread "main" java.lang.ClassCastException

Since this query returns an Object[] I can not cast it.

How can I get rows and cast them to Places objects

Mapping

<?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>
    <class name="com.places.general.Place" table="PLACES" schema="dbo">
        <id name="id" type="int">
            <column name="ID"/>
            <generator class="identity"/>
        </id>
        <property name="name" type="string">
            <column length="100" name="NAME" not-null="true"/>
        </property>
    </class>
</hibernate-mapping>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:23:51+00:00Added an answer on June 12, 2026 at 8:23 am

    In the HQL statement you have to use the Java class name, not the table name:

    List places = session.createQuery("FROM Place").list();
    

    or

    List places = session.createQuery("FROM com.places.general.Place").list();
    

    And your second try returns a list of arrays of Object, each array first element is id, second is name, which have to be casted to Integer and String separately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to use the hibernate dialect for SQLite from http://code.google.com/p/hibernate-sqlite/ in
Trying to use nhibernate on sql-server compact edition for unit testing and having some
I'm running NHibernate and SQL Server CE I am trying to use GUIDs as
I have a problem with changing my spring/hibernate application from MySql to SQL Server.
trying to use hibernate with my web app and getting following exception: Initial SessionFactory
Im trying to convert the Vaadin demo for JPAContainer to use hibernate instead of
I am trying to use Envers on a project that also uses Hibernate and
In Spring MVC Hibernate application , when i am trying to use properties file
I am trying to obtain Spring-defined Hibernate Configuration and SessionFactory objects in my non-Spring
Why amd I getting warnings from Log4J 1.2.17? I am trying to use log4j

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.