为什么在使用JPA执行基本mySQL搜索时出现此错误?

时间:2014-03-27 12:02:17

标签: java mysql jpa

我正在查询我的sql数据库。我有两张桌子:

表1: BusinessAccount

businessName (String);
businessAddress (String);
county (int); (county is the Foreign Key for countyId on table 2)

表2:

countyId (int);
countyName (String);

我正在尝试返回县= 1的所有商家帐户的列表。我收到一条错误说明如下:

  

您试图为类型java.lang.Integer设置值   具有预期类型的​​参数CountyId   来自查询字符串
com.needABuilder.entities.County   SELECT u FROM BusinessAccount u WHERE u.county=:CountyId

我不明白为什么我收到此错误,因为我在BusinessAccount表中搜索整数值county = 1的条目。这是我的代码。谢谢你的帮助。

public List<BusinessAccount> searchContractors() {
    factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    EntityManager em = factory.createEntityManager();   
    List<BusinessAccount> contractorList = new ArrayList<BusinessAccount>();

    em.getTransaction().begin();
    int countyId=1;
    Query myQuery = em.createQuery(
                    "SELECT u FROM BusinessAccount u WHERE u.county=:CountyId");
    myQuery.setParameter("CountyId", countyId);

    contractorList=myQuery.getResultList();
    em.getTransaction().commit();
    em.close();

    return contractorList;
}

1 个答案:

答案 0 :(得分:2)

该消息解释了一切。 BusinessAccount.county的类型为County。它不是Integer类型。您无法将CountyInteger进行比较。

您可以将县与另一个县进行比较,或将整数与另一个整数进行比较。那么你想要的是什么(假设County.id属于Integerint

select u from BusinessAccount u where u.county.id = :countyId