File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error running HQL query in Hibernate 3.1

 
Sameer Kunder
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam trying below HQL select query but getting Error Message while executing the SQL_QUERY01, where as when iam trying SQL_QUERY02 it works fine, can someone please help me to understand why SQL_QUERY01 not working. I have all ORM mapping correctly but error message says could not resolve property: ID

Iam using Hibernate 3.1 with spring 3.1 framework.

String SQL_QUERY01="Select contact.ID, contact.FIRSTNAME from Contact contact";

//String SQL_QUERY02 ="from Contact contact";

Error MEssage:
------------------
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
could not resolve property: ID of: com.Sam.web.Contact [Select contact.ID, contact.FIRSTNAME from com.Sam.web.Contact contact]



SelectClauseExample.java
-------------------------------
package com.Sam.web;

import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;

import java.util.*;

public class SelectClauseExample {



public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();

Contact contact = new Contact();

//Create Select Clause HQL

//Using from Clause
String SQL_QUERY01="Select contact.ID, contact.FIRSTNAME from Contact contact";

//String SQL_QUERY02 ="from Contact contact";

Query query = session.createQuery(SQL_QUERY01);

for(Iterator it=query.iterate();it.hasNext();){
contact=(Contact)it.next();
System.out.println("ID: " + contact.getId());
System.out.println("First Name: " + contact.getFirstName());
}

session.close();

}catch(Exception e){
System.out.println(e.getMessage());
}finally{
/*finally block goes here;*/
}

}

}

contact.hbm.xml
----------------
<hibernate-mapping default-cascade="none" default-access="property" default-lazy="true" auto-import="true">
<class name="com.Sam.web.Contact" table="CONTACT" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version">
<id name="id" type="int" column="ID">
<generator class="assigned" />
</id>
<property name="firstName" unique="false" optimistic-lock="true" lazy="false" generated="never">
<column name="FIRSTNAME" />
</property>
<property name="lastName" unique="false" optimistic-lock="true" lazy="false" generated="never">
<column name="LASTNAME" />
</property>
<property name="email" unique="false" optimistic-lock="true" lazy="false" generated="never">
<column name="EMAIL" />
</property>
</class>
</hibernate-mapping>


Contact.java
--------------

public class Contact {

private String firstName;
private String lastName;
private String email;
private int id;
// private String create_date;

public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public int getId() {
return id;
}

public void setFirstName(String string) {
firstName = string;
}
public void setLastName(String string) {
lastName = string;
}
public void setEmail(String string) {
email = string;
}
public void setId(int l) {
id = l;
}
}
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you put your code inside of code tags? It is much easier to read that way. For example:

SelectClauseExample.java
-------------------------------


contact.hbm.xml
----------------


Contact.java
--------------
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


did you know that Java is case sensitive?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the complete exception?
 
Hemant Thard
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:Can you post the complete exception?


Why do you want complete Exception. Tim has already posted the answer.
 
Sameer Kunder
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim, it worked,. I was using ID instead of id in my code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic