我使用Spring mvc和Hibernate和AliasToBeanResultTransformer。当我从jsp获取数据时,我收到了SQL的异常。
模特课:
@Entity
@Table(name="CARS")
public class Car {
@Id
@Column(name="ID", nullable=false, unique=true)
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="NAME", length=50)
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
此方法从服务获取数据:
public List<Car> getAllCars() {
Session session = HibernateUtils.getSession(sessionFactory);
String sqlQuery = GetSqlUtils.getSqlQueryString(CarRepositoryImpl.class, SQL_DIR + GET_ALL_CARS);
List<Car> list = null;
try {
Query query = session.createSQLQuery(sqlQuery);
list = query.setResultTransformer(new AliasToBeanResultTransformer(Car.class)).list();
} catch (HibernateException e) {
logger.error("error at CarRepositoryImpl.getAllCars: " + e.getMessage());
} finally {
HibernateUtils.closeCurrentSession(session);
}
return list;
}
SQL:
SELECT CAR.ID AS ID, CAR.NAME AS NAME
FROM
CARS CAR
记录消息:
CarRepositoryImpl - error at CarRepositoryImpl.getAllCars: Could not find setter for ID on class com.tct.web.model.Car
如何解决此错误?非常感谢!
答案 0 :(得分:1)
将int
更改为Integer
以获取ID
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
答案 1 :(得分:1)
您的字段以小写形式声明,并使用大写别名。
尝试更改它们,使它们与字段名称完全相同:
SELECT CAR.ID AS id, CAR.NAME AS name