| Author |
Error running HQL query in Hibernate 3.1
|
Sameer Kunder
Greenhorn
Joined: May 22, 2012
Posts: 5
|
|
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
Joined: Apr 30, 2003
Posts: 819
|
|
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
Joined: Apr 30, 2003
Posts: 819
|
|
did you know that Java is case sensitive?
|
 |
Kunal Lakhani
Ranch Hand
Joined: Jun 05, 2010
Posts: 609
|
|
|
Can you post the complete exception?
|
kunal
|
 |
Hemant Thard
Ranch Hand
Joined: Dec 23, 2008
Posts: 119
|
|
Kunal Lakhani wrote:Can you post the complete exception?
Why do you want complete Exception. Tim has already posted the answer.
|
 |
Sameer Kunder
Greenhorn
Joined: May 22, 2012
Posts: 5
|
|
|
Thanks Tim, it worked,. I was using ID instead of id in my code.
|
 |
 |
|
|
subject: Error running HQL query in Hibernate 3.1
|
|
|