• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Hibernate mapping

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
every time I make a call to method getAllUsers, I get the following error. I'm using MySQL and Hibernate 3.0. Can any body help,I have tried
everything

javax.servlet.ServletException: org.hibernate.hql.ast.QuerySyntaxException: userdetails is not mapped [from userdetails]
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)


Code Used
----------
MySQL Database:studentdb
Table:userdetails
userID INT(10)
firstname VARCHAR(256)
lastname VARCHAR(256)
age INT(10)



package com.student.dao;

import java.util.List;

import com.student.dao.HibernateUtil;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.student.vo.UserDetailsVO;


public class UserDetailsHibernateDao implements UserDetailsDao {

private List<UserDetailsVO> userList;
private UserDetailsVO user;

public List<UserDetailsVO> getAllUsers() {
Session session = HibernateUtil.getSession();
try {
session.beginTransaction();
userList = session.createQuery("from userdetails").list();
return userList;
}catch (HibernateException e) {
throw e;
} finally {
session.close();
}
}
}


File:hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/studentdb</property>
<property name="connection.username">root</property>
<property name="connection.password">student</property>
<!-- 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource="com/student/vo/UserDetailsVO.hbm.xml"/>
</session-factory>
</hibernate-configuration>


File:UserDetailsVO.hbm.xml

<?xml version="1.0"?>
<!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.student.vo.UserDetailsVO" table="userdetails">
<id
name="userID"
type="integer"
column="userID"
length="10"
>
<generator class="native" />
</id>
<property
name="age"
type="integer"
column="age"
length="10"
/>
<property
name="firstName"
type="string"
column="firstname"
length="256"
/>
<property
name="lastName"
type="string"
column="lastname"
length="256"
/>
</class>
</hibernate-mapping>



File:UserDetailsVO.java

package com.student.vo;

import java.io.Serializable;

public class UserDetailsVO implements Serializable{
private Integer userID;
private Integer age;
private String firstName;
private String lastName;

public UserDetailsVO() {
}

public UserDetailsVO(Integer userID, String firstName, String lastName, Integer age) {
this.userID = userID;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}

public Integer getUserID() {
return userID;
}

public void setUserID(Integer userID) {
this.userID = userID;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}
}
 
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the HQL "from" clause, one has to use the name of the Java class, and not the name of the database table.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When youu fire your HQL query you should not refer to the table or its columns. Instead you should use the mapped class and their properties.
try
 
reply
    Bookmark Topic Watch Topic
  • New Topic