我正在尝试向我找到的项目添加一些功能。有查询选择指定的结果。现在我正在尝试获取第二个查询的结果。这是我的代码:
汽车.java
@Entity
@NamedQueries({
@NamedQuery(name = "car.unsold", query = "Select c from Car c where c.sold = false"),
@NamedQuery(name="car.findByName", query=" SELECT C FROM Car c where c.make=?")
})
public class Car {
private Long id;
private String make;
private String model;
//and all getters and setters
}
CarManager.java
@Stateless
public class CarManager {
@PersistenceContext
EntityManager em1;
@SuppressWarnings("unchecked")
public List<Car> getAvailableCars() {
return em1.createNamedQuery("car.unsold").getResultList();
}
@SuppressWarnings("unchecked")
public List<Car> getCarsByName(String make){
return em1.createNamedQuery("car.findByName").setParameter(1, make).getResultList();
}}
CarBean.java
@SessionScoped
@Named("carBean")
public class CarFormBean implements Serializable {
private static final long serialVersionUID = 1L;
private Car car = new Car();
private ListDataModel<Car> cars = new ListDataModel<Car>();
private ListDataModel <Car> searchresult = new ListDataModel<Car>();
private String searchCar;
public String getSearchCar() {
return searchCar;
}
public void setSearchCar(String searchCar) {
this.searchCar = searchCar;
}
@Inject
private CarManager cm;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public ListDataModel<Car> getAvailableCars() {
cars.setWrappedData(cm.getAvailableCars());
return cars;
}
public ListDataModel<Car> getCarsByName(){
searchresult.setWrappedData(cm.getCarsByName(searchCar));
return searchresult;
}
public String searchCar(){
cm.getCarsByName(searchCar);
return null;
}
我的问题是如何显示 carByName 查询的结果?上面的代码可以吗?第一次查询的结果由以下人员打印:
<rich:dataTable value="#{carBean.availableCars}" var="car"
但它是如何完成的?我看不到名为 availableCars 的方法。
更新 Search.xhtml http://wklej.org/id/921560/ all.xhtml http://wklej.org/id/921561/