Sakura_ZHJ 2015-09-21 01:13 采纳率: 20%
浏览 2127

SSH整合,hibernate注解配置,无hbm.xml,映射出现问题

java实体类代码:

 package cn.com.car.base.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;



@Entity
@Table(name="T_CAR")
public class Car implements Serializable{
    private static final long serialVersionUID = 1L;

    @GenericGenerator(name = "generator", strategy = "guid")
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "id", unique = true, nullable = false, length = 38)
    private String id;

    @Column(name="models")
    private String models;//具体车型

    @Column(name="type")
    private String type;//所属类别


    @Column(name="price")
    private int price;//价格


    public String getId() {
        return id;
    }


    public void setId(String id) {
        this.id = id;
    }


    public String getModels() {
        return models;
    }


    public void setModels(String models) {
        this.models = models;
    }


    public String getType() {
        return type;
    }


    public void setType(String type) {
        this.type = type;
    }


    public int getPrice() {
        return price;
    }


    public void setPrice(int price) {
        this.price = price;
    }


    @Override
    public String toString() {
        return "Car [id=" + id + ", models=" + models + ", type=" + type
                + ", price=" + price + "]";
    }


}


application.xml配置

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
       default-autowire="byName" default-lazy-init="true">

    <!-- 属性文件读入 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:hibernate.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${hibernate.connection.driverClassName}"/>
        <property name="url" value="${hibernate.connection.url}"/>
        <property name="username" value="${hibernate.connection.username}"/>
        <property name="password" value="${hibernate.connection.password}"/>
    </bean>

    <!--Hibernate SessionFatory-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="annotatedClasses">
            <list>
                <value>cn.com.car.base.entity.Car</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>classpath:hibernate.properties</value>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>


</beans>

hibernate.properties配置文件

 hibernate.show_sql=true
hibernate.format_sql=false
hibernate.cache=org.hibernate.cache.EhCacheProvider
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.connection.driverClassName=com.mysql.jdbc.Driver

hibernate.connection.url=jdbc:mysql://localhost:3306/car

hibernate.connection.username=car
hibernate.connection.password=123456

hibernate.c3p0.minPoolSize=5
hibernate.c3p0.maxPoolSize=50
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statement=200

DAO层

 package cn.com.car.base.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.com.car.base.dao.CarDao;
import cn.com.car.base.entity.Car;

public class CarDaoImpl extends HibernateDaoSupport implements CarDao {

    @Override
    public List<Car> getAllCar() {
        String sql="from T_CAR where 1=1";
        List<Car> list=this.getHibernateTemplate().find(sql);
        return list;
    }

    @Override
    public List<Car> getCarByParam(HashMap<String, String> param) {
        StringBuilder sql=new StringBuilder();
        sql.append("select * from T_CAR where 1=1");
        if(param!=null){
            if(param.get("type")!=null){
                String type=param.get("type");
                sql.append("and type="+type);
            }
            if(param.get("price")!=null){
                String price=param.get("price"); 
                sql.append("and price="+price);
            }


            Query query = this.getSession().createSQLQuery(sql.toString());
            List<Car> list=query.list();
            return list;
        }
        List<Car> list=this.getHibernateTemplate().find(sql.toString());
        return list;
    }

    @Override
    public Car getCarByModels(String models) {
        String hql = "from T_CAR t where 1=1 "
                  +" and t.models=? ";
            Query query = this.getSession().createQuery(hql).setString(0, models);
            return (Car) query.list().get(0);
    }

    @Override
    public Car getCarById(String id) {
        String hql = "from T_CAR t where 1=1 "
                  +" and t.id=? ";
            Query query = this.getSession().createQuery(hql).setString(0, id);
            return (Car) query.list().get(0);
    }

}

我是模仿别的项目搭建的,没有.hbm.xml,现在运行DAO层就出现
“org.hibernate.hql.ast.QuerySyntaxException: T_CAR is not mapped [from T_CAR where 1=1]”这样的错误。

  • 写回答

3条回答 默认 最新

  • oxcow 2015-09-21 02:37
    关注

    很明显,你的这两句HQL语句有问题

     @Override
        public Car getCarByModels(String models) {
            String hql = "from T_CAR t where 1=1 "
                      +" and t.models=? ";
                Query query = this.getSession().createQuery(hql).setString(0, models);
                return (Car) query.list().get(0);
        }
    
        @Override
        public Car getCarById(String id) {
            String hql = "from T_CAR t where 1=1 "
                      +" and t.id=? ";
                Query query = this.getSession().createQuery(hql).setString(0, id);
                return (Car) query.list().get(0);
        }
    

    既然你创建的是createQuery 那么必须使用hql语法,因此

     String hql = "from T_CAR t where 1=1 "
    

    这个语句中的 from后面应该跟的是对象,也就是表 T_CAR对应的实体 Car 。 HQL语句中所有的属性对应的不是表字段,而是表对应实体的属性,
    因此 改成

     String hql = "from Car t where 1=1 "
    

    就行了。下面这段代码不会报错

      @Override
        public List<Car> getCarByParam(HashMap<String, String> param) {
            StringBuilder sql=new StringBuilder();
            sql.append("select * from T_CAR where 1=1");
            if(param!=null){
                if(param.get("type")!=null){
                    String type=param.get("type");
                    sql.append("and type="+type);
                }
                if(param.get("price")!=null){
                    String price=param.get("price"); 
                    sql.append("and price="+price);
                }
    
    
                Query query = this.getSession().createSQLQuery(sql.toString());
                List<Car> list=query.list();
                return list;
            }
            List<Car> list=this.getHibernateTemplate().find(sql.toString());
            return list;
        }
    

    是因为使用了createSQLQuery所以运行的是原生SQL,

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100