Java JPA错误;您试图从查询字符串中设置类型类数据的期望类型int的值

时间:2018-04-01 23:23:50

标签: java jpa

我有以下java JPA项目的错误。谁能帮助我?谢谢。

类型例外报告

消息您试图为参数cityMakeMakeID设置类型类data.CityMake的值,期望类型为int from query string选择f from Foodmodel f WHERE f.cityMakeMakeID =:cityMakeMakeID order by f .foodModelName。

说明服务器遇到意外情况,无法完成请求。

异常

java.lang.IllegalArgumentException:您试图为参数cityMakeMakeID设置类型类data.CityMake的值,期望类型为int from query string选择f from Foodmodel f WHERE f.cityMakeMakeID =:cityMakeMakeID order by f.foodModelName

org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:933)

org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:593)

dao.FoodModelDAO.getModels(FoodModelDAO.java:69)

servlets.StartServlet.processRequest(StartServlet.java:67)

servlets.StartServlet.doGet(StartServlet.java:86)

javax.servlet.http.HttpServlet.service(HttpServlet.java:635)

javax.servlet.http.HttpServlet.service(HttpServlet.java:742)

org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

注意服务器日志中提供了根本原因的完整堆栈跟踪。

-------------------------------- ------------ FoodModelDAO.java ---------

公共类FoodModelDAO {

  public static List<Foodmodel> getModels(int makeid)
  {

    CityMake make = CityMakeDAO.findMakeById(makeid);

    EntityManager em = DBUtil.getEmFactory().createEntityManager();
    String qString = "Select f from Foodmodel f " +
                     "WHERE f.cityMakeMakeID = :cityMakeMakeID  " +
                     "order by f.foodModelName";

    TypedQuery<Foodmodel> query = em.createQuery(qString, Foodmodel.class);

      query.setParameter("cityMakeMakeID", make );

    try
    {
        List<Foodmodel> models = query.getResultList();
        return models;
    }

    catch(NoResultException e) 
    {
        return new ArrayList<Foodmodel>();
    }
    finally 
    {
        em.close();
    }
  }

}

--------------- FoodModel.java post 4/2/2018 -----------------------     我再次更新了FoodModel的帖子。

enter code here




@Entity
public class FoodModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long    foodModelId;
private String  foodModelName;


@OneToOne
private CityMake cityMakeMakeID;

public FoodModel() {

    foodModelName = "";

}

public FoodModel(Long m, String v, CityMake makeObj) {
    foodModelId = m;
    foodModelName = v;
    //citymake = makeObj;
    cityMakeMakeID = makeObj;
}

public Long getModelId() {
    return foodModelId;
}

public void setModelId(Long id) {
    foodModelId = id;
}

public String getModelName() {
    return  foodModelName;
}

public void setModelName(String value) {
    this.foodModelName = value;
}

public CityMake getMake() {
    //return citymake;
    return cityMakeMakeID;
}

public void setMake(CityMake make) {
    //this.citymake = make;
    this.cityMakeMakeID = make;

 }

}

1 个答案:

答案 0 :(得分:0)

    query.setParameter("cityMakeMakeID", makeid);

为什么?您正在为Foodmodel.class创建一个类型化查询,因此参数类型应与模型相对应(查看FoodModel,该属性定义为int)。

    @Column(name = "CityMake_MakeID")
    private int cityMakeMakeID;

如果您的模型是这样定义的,那么您的工作方式应该有效:

    @OneToOne //Or any type of relation that matches your needs
    private CityMake cityMakeMakeID; //See the variable Type