Reputation: 515
I'm using Spring mvc and Hibernate with AliasToBeanResultTransformer. When I get data from jsp, I received an exception from SQL.
Model class:
@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;
}
}
This method get data from service:
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
Log message:
CarRepositoryImpl - error at CarRepositoryImpl.getAllCars: Could not find setter for ID on class com.tct.web.model.Car
How to fix this error ? thank so much !
Upvotes: 0
Views: 258
Reputation: 26572
Your fields are declared in lowercase and you use uppercase aliases.
Try to change them so they are exactly as the field names:
SELECT CAR.ID AS id, CAR.NAME AS name
Upvotes: 1
Reputation: 3890
change int
to Integer
for id
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
Upvotes: 1