org.hibernate.hql.ast.QuerySyntaxException:UserType未映射[来自UserType]

时间:2014-08-19 04:52:50

标签: java spring hibernate

例外是关于表没有映射到类,但我无法调试出错的地方。

我检查了xml配置。该类是UserType,数据库表也称为UserType。

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;
}

}

我的 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>

我感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

问题在于hibernate映射配置:

  • name :应与类字段名称和
  • 相同
  • column :是数据库表列的名称,或者是hibernate创建表时要设置的列名。

注意:映射文件指示Hibernate如何将定义的类映射到数据库表。

您的UserType.hbm.xml应如下所示:

<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>

答案 1 :(得分:0)

事实证明这与HQL问题有关:不使用UserType,而是使用com.howtodojava.entity.UserType;

并且错误消息将消失。