Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA Hibernate Projections.property的替代方案_Java_Hibernate_Jpa - Fatal编程技术网

Java JPA Hibernate Projections.property的替代方案

Java JPA Hibernate Projections.property的替代方案,java,hibernate,jpa,Java,Hibernate,Jpa,伙计们 试图限制从DB中获取的列数,但发现以下情况: 因此,似乎可以通过JPA的hibernate扩展来实现这一点?JPA中没有替代语法,hibernate是JPA的超集,它提供了JPA规范之外的功能。 在JPA中,您必须使用select语句(在您的例子中是multiselect语句)显式地单独进行投影 条件查询 // Create instance of CriteriaBuilder, where em is EntityManager CriteriaBuilder cb = em.ge

伙计们

试图限制从DB中获取的列数,但发现以下情况:


因此,似乎可以通过JPA的hibernate扩展来实现这一点?

JPA中没有替代语法,hibernate是JPA的超集,它提供了JPA规范之外的功能。 在JPA中,您必须使用select语句(在您的例子中是multiselect语句)显式地单独进行投影

条件查询

 // Create instance of CriteriaBuilder, where em is EntityManager
CriteriaBuilder cb = em.getCriteriaBuilder();      

  // Use CriteriaBuilder interface to create an instance 
  // of CriteriaQuery. For multiselect result set is Object [].
CriteriaQuery<Object[]> c = cb.createQuery(Object[].class);

   // Establish the root of the query by invoking from() to get back a Root object. 
Root<User> user = c.from(User.class); 

  // Establish the SELECT clause of the query by passing the root into the multiselect() method
criteriaQuery.multiselect(user.get("property1"), user.get("property2"));

谢谢你的建议!我刚刚尝试了您的解决方案..但是:在第一种情况下,执行的结果(getResultList)只是一个对象数组-因此结果不会映射回用户类。。第二种方法给出了*QuerySyntaxException:无法在类*上找到合适的构造函数。对于第二种方法,您需要有合适的构造函数。Multiselect将返回对象[]或元组。我还将在示例中包括元组。谢谢,但仍然是相同的-返回的列表不是用户类型。在Tuple中,我注意到了有趣的行为。每个元组都有一个成员属性,-在我们的例子中是
id
name
。但它不会自动转换回用户类型。应该这样吗?我仍然看到生成的SQL是正确的,但是结果没有映射到用户实体。似乎我真的误解了什么。multiselect的结果集是对象数组,single select将返回持久化实体。例如
CriteriaQuery c=cb.createQuery(User.class);Root用户=c.from(user.class);c、 选择(user.get(“property1”)@fg78nc我也在试图找到解决方案,但如果我的选择列是动态的,并且上面的代码是一个数据提供程序调用,例如findUser(一些参数),现在如果我的用户表有70列,并且使用相同的findUser()方法我在一个应用程序位置需要10列,而在另一个位置需要获取12列,那么我如何实现这一点呢
Hibernate offers an older, legacy org.hibernate.Criteria API which should be considered deprecated. No feature development will target those APIs. Eventually, Hibernate-specific criteria features will be ported as extensions to the JPA javax.persistence.criteria.CriteriaQuery. For details on the org.hibernate.Criteria API, see Legacy Hibernate Criteria Queries.
 // Create instance of CriteriaBuilder, where em is EntityManager
CriteriaBuilder cb = em.getCriteriaBuilder();      

  // Use CriteriaBuilder interface to create an instance 
  // of CriteriaQuery. For multiselect result set is Object [].
CriteriaQuery<Object[]> c = cb.createQuery(Object[].class);

   // Establish the root of the query by invoking from() to get back a Root object. 
Root<User> user = c.from(User.class); 

  // Establish the SELECT clause of the query by passing the root into the multiselect() method
criteriaQuery.multiselect(user.get("property1"), user.get("property2"));
CriteriaQuery<User> c = cb.createQuery(User.class);
Root<User> user = c.from(User.class);
c.select(cb.construct(User.class,
                      user.get("property1"),
                      user.get("property2")));
    CriteriaQuery<Tuple> c = cb.createTupleQuery();
    Root<User> user = c.from(User.class);
    c.select(cb.tuple(user.get("property1"), user.get("property2")));
    Query query = entityManager.createQuery(c);
    List<Tuple> results = query.getResultList();
    c.select(cb.array(user.get("property1"), user.get("property2")));