我有一个名为 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