java - org.hibernate.hql.ast.QuerySyntaxException: UserType is not mapped [from UserType] -
the exception table not mapping class, failed debug went wrong.
i have checked xml configurations. class usertype , database table called usertype well.
usertype.java:
package com.howtodoinjava.entity; public class usertype { private int _usertypeid; private string _usertypename; private boolean _typeissuperuser; public int getusertypeid() { return _usertypeid; } public string getusertypename() {return _usertypename; } public boolean gettypeissuperuser() {return _typeissuperuser; } public usertype setusertypeid(int id) { _usertypeid = id; return this; } public usertype setusertypename(string name) { _usertypename = name; return this; } public usertype settypeissuperuser(boolean issuperuser) { _typeissuperuser = issuperuser; return this; } } usertypedaoimpl.java:
package com.howtodoinjava.dao; import java.util.list; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.repository; import com.howtodoinjava.entity.usertype; @repository public class usertypedaoimpl implements usertypedao { @autowired private sessionfactory sessionfactory; public void add(usertype ut) { this.sessionfactory.getcurrentsession().save(ut); } @suppresswarnings("unchecked") public list<usertype> getall() { return this.sessionfactory.getcurrentsession().createquery("from usertype").list(); } public void delete(integer id) { usertype ut= (usertype) sessionfactory.getcurrentsession().load( usertype.class, id); if (null != ut) { this.sessionfactory.getcurrentsession().delete(ut); } } } editusertypecontroller.java:
@controller public class editusertypecontroller{ @autowired private usertypemanager usertypemanager; @requestmapping(value = "/", method = requestmethod.get) public string list(modelmap map) { map.addattribute("usertype", new usertype()); map.addattribute("usertypelist", usertypemanager.getall()); return "editeusertypelist"; } @requestmapping(value = "/add", method = requestmethod.post) public string addusertype(@modelattribute(value="ut") usertype ut, bindingresult result) { usertypemanager.add(ut); return "redirect:/"; } @requestmapping("/delete/{id}") public string deleteusertype(@pathvariable("id") integer id) { usertypemanager.delete(id); return "redirect:/"; } public void setusertypemanager(usertypemanager new_utm) { this.usertypemanager = new_utm; } }
my usertype-servlet.xml:
<?xml version="1.0" encoding="utf-8"?> <beans> <context:annotation-config /> <context:component-scan base-package="com.howtodoinjava.controller" /> <bean id="jspviewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="viewclass" value="org.springframework.web.servlet.view.jstlview" /> <property name="prefix" value="/web-inf/view/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="messagesource" class="org.springframework.context.support.reloadableresourcebundlemessagesource"> <property name="basename" value="classpath:messages" /> <property name="defaultencoding" value="utf-8" /> </bean> <bean id="propertyconfigurer" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer" p:location="/web-inf/jdbc.properties" /> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close" p:driverclassname="${jdbc.driverclassname}" p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> <bean id="sessionfactory" class="org.springframework.orm.hibernate3.localsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="configlocation"> <value>classpath:hibernate.cfg.xml</value> </property> <property name="configurationclass"> <value>org.hibernate.cfg.annotationconfiguration</value> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">${jdbc.dialect}</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="usertypedao" class="com.howtodoinjava.dao.usertypedaoimpl"></bean> <bean id="usertypemanager" class="com.howtodoinjava.service.usertypemanagerimpl"> </bean> <tx:annotation-driven /> <bean id="transactionmanager" class="org.springframework.orm.hibernate3.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory" /> </bean> </beans> 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> <mapping resource="usertype.hbm.xml" /> </session-factory> </hibernate-configuration> usertype.hbm.xml:
<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd//en" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.howtodoinjava.entity.usertype" table="usertype"> <meta attribute="class-description"> user type </meta> <id name="_usertypeid" column="usertypeid" type="int"> <generator class="native"/> </id> <property name="_usertypename" column="usertypename" type="string"/> <property name="_typeissuperuser" column="typeissuperuser" type="string"/> </class> </hibernate-mapping> i appreciate helps!
the problem in hibernate mapping configuration:
- name: should same class field name ,
- column: name of database table column, or column name set while table creation hibernate.
note: mapping file instructs hibernate how map defined class database table.
your usertype.hbm.xml should like:
<hibernate-mapping> <class name="com.howtodoinjava.entity.usertype" table="usertype"> <meta attribute="class-description"> user type </meta> <id name="_usertypeid" column="usertypeid" type="int"> <generator class="native"/> </id> <property name="_usertypename" column="usertypename" type="string"/> <property name="_typeissuperuser" column="typeissuperuser" type="string"/> </class> </hibernate-mapping>
Comments
Post a Comment