问题描述
- 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]”这样的错误。
解决方案
很明显,你的这两句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,
解决方案二:
hibernate.hbm.xml配置
hibernate.hbm.xml配置
hibernate 的hbm.xml配置
解决方案三:
语句有问题,你的T_CAR 改成你自己的表名
解决方案四:
用SSH一般很少用到SQL,大多都是HQL
时间: 2024-10-01 05:42:33