wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate mapping" Watch "Hibernate mapping" New topic
Author

Hibernate mapping

Dave James
Greenhorn

Joined: Jun 18, 2001
Posts: 5
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;
}
}
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
In the HQL "from" clause, one has to use the name of the Java class, and not the name of the database table.
Ankita Jha
Ranch Hand

Joined: Sep 15, 2008
Posts: 55
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
 
wood burning stoves
 
subject: Hibernate mapping
 
Similar Threads
Hibernate Repeated column mapping problem
session.flush(); gives null
problem with hibernate
Annotations exception
Caching Issues with Hibnernate