我正在查询我的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;
}
答案 0 :(得分:2)
该消息解释了一切。 BusinessAccount.county
的类型为County
。它不是Integer
类型。您无法将County
与Integer
进行比较。
您可以将县与另一个县进行比较,或将整数与另一个整数进行比较。那么你想要的是什么(假设County.id
属于Integer
或int
)
select u from BusinessAccount u where u.county.id = :countyId