我有一个名为Service的域对象,其中包含一个id字段:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "type")
private Service.ServiceTypes serviceType;
在我的DAO实现中,我可以通过serviceType轻松查询:
@Override
public Set<Service> queryDatabaseByServiceType(ServiceTypes serviceType) {
logger.debug("Querying the database by service type " + serviceType.toString());
return new HashSet<Service>(super.entityManager.createQuery("from Service s where s.type = :serviceType").setParameter("serviceType", serviceType).getResultList());
}
当我复制并通过该方法并尝试针对“id”运行查询时,我得到零结果。
@Override
public Service queryDatabaseByServiceId(int id) {
List<Service> results = super.entityManager.createQuery("from Service s where s.id = :id").setParameter("id", id).getResultList();
return results.get(0);
总是没有回报......我已经尝试了一百万种不同的方式......
CriteriaBuilder builder = super.entityManager.getCriteriaBuilder();
CriteriaQuery<Service> criteria = builder.createQuery(Service.class);
Root<Service> s1 = criteria.from(Service.class);
TypedQuery<Service> query = super.entityManager.createQuery(
criteria.select(s1).where(builder.equal(s1.get("id"), id)));
Service service = query.getSingleResult();
return service;
仍然没有。我不知道为什么我可以查询其他列而不是ID。我已经验证了我传入的ID是有效的,没有任何作用。
有什么想法吗?
编辑:以下是我尝试查询的其他方法:
Service ser = super.entityManager.find(Service.class, new Integer(1));
List z1 = super.entityManager.createQuery("select s.id from Service s").getResultList();
List results1 = super.entityManager.createQuery("SELECT s FROM Service s WHERE id = 1").getResultList();
List<Service> results = super.entityManager.createQuery("from Service as s where s.id = :id").setParameter("id", new Integer(1)).getResultList();
我已经尝试将我的域对象中的字段更改为“idx”而不是“id”,我已经尝试将列名更改为“service_id”,以为可能在它用于id字段的内容之间存在一些内部冲突和我的对象,但仍然没有。如前所述和评论中所述,任何其他字段的查询工作正常,我可以轻松获得显示ID的结果:
return new HashSet<Service>(super.entityManager.createQuery("from Service s where s.type = :serviceType").setParameter("serviceType", serviceType).getResultList());
生成一个填充的列表,当转换为json时,我在浏览器中看到了这个:
{"services":[{"id":2,"serviceType":"x",.....
对于所有这些不同类型的查询,这里是日志输出:
DEBUG: service.impl.ServiceServiceImpl - Querying the database by service id 2
Hibernate: select service0_.service_id as service1_0_, service0_.description as descript2_0_, service0_.name as name0_, service0_.type as type0_, service0_.tmodel as tmodel0_, service0_.uddi_id as uddi6_0_, service0_.url as url0_, service0_.version as version0_ from services service0_ where service0_.service_id=?
TRACE: org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [INTEGER] - 1
INFO : service.impl.ServiceServiceImpl - results list from id query: 0
Hibernate: select service0_.service_id as service1_0_0_, service0_.description as descript2_0_0_, service0_.name as name0_0_, service0_.type as type0_0_, service0_.tmodel as tmodel0_0_, service0_.uddi_id as uddi6_0_0_, service0_.url as url0_0_, service0_.version as version0_0_ from services service0_ where service0_.service_id=?
TRACE: org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [INTEGER] - 1
Hibernate: select service0_.service_id as col_0_0_ from services service0_
INFO : service.impl.ServiceServiceImpl - z1 count: 0
Hibernate: select service0_.service_id as service1_0_, service0_.description as descript2_0_, service0_.name as name0_, service0_.type as type0_, service0_.tmodel as tmodel0_, service0_.uddi_id as uddi6_0_, service0_.url as url0_, service0_.version as version0_ from services service0_ where service0_.service_id=1
INFO : service.impl.ServiceServiceImpl - results1 from id query: 0
Hibernate: select service0_.service_id as service1_0_, service0_.description as descript2_0_, service0_.name as name0_, service0_.type as type0_, service0_.tmodel as tmodel0_, service0_.uddi_id as uddi6_0_, service0_.url as url0_, service0_.version as version0_ from services service0_ where service0_.service_id=2
INFO : service.impl.ServiceServiceImpl - results2 from id query: 0
Hibernate: select service0_.service_id as service1_0_0_, service0_.description as descript2_0_0_, service0_.name as name0_0_, service0_.type as type0_0_, service0_.tmodel as tmodel0_0_, service0_.uddi_id as uddi6_0_0_, service0_.url as url0_0_, service0_.version as version0_0_ from services service0_ where service0_.service_id=?
TRACE: org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [INTEGER] - 2
答案 0 :(得分:1)
我们在项目中使用JPA2,但它没有这种问题,如何将“public Service queryDatabaseByServiceId(int id){”id类型更改为Integer,在我看来,在hibernate中Integer将是一个更好的选择。让我知道发生了什么。另一件事是更改log4j以打印sql以及参数值来检查它。
<logger name="org.hibernate.type">
<level value="trace"></level>
</logger>
<logger name="org.hibernate.sql">
<level value="trace"></level>
</logger>