Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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 JPQL-一对多计数的左连接_Java_Count_Left Join_Jpql_Spring Data - Fatal编程技术网

Java JPQL-一对多计数的左连接

Java JPQL-一对多计数的左连接,java,count,left-join,jpql,spring-data,Java,Count,Left Join,Jpql,Spring Data,花了大约2个小时试图理解为什么JPQL查询没有返回我所期望的结果。请考虑代码: System.out.println("JPQL ----------------------------"); Query q = em.createQuery( "select u.userName, count(p.id) from User u " + "left join u.posts p group by u.userName");

花了大约2个小时试图理解为什么JPQL查询没有返回我所期望的结果。请考虑代码:

    System.out.println("JPQL ----------------------------");
    Query q = em.createQuery(
            "select u.userName, count(p.id) from User u " + 
            "left join u.posts p group by u.userName");
    List x = q.getResultList();     
    for(Object o : x) {
        Object[] y = (Object[])o;
        System.out.printf("%s %s\n", y[0], y[1]);
    }

    System.out.println("Spring Data JPA -----------------");
    for(User user : userRepository.findAll()) {
        List<Post> posts = postRepository.findAllByAuthor(user);
        System.out.printf("%s %s\n", user.getUserName(), posts.size());
    }
我希望JPQL方法可以像存储库方法一样打印。哪里错了

更新

以下是SQL跟踪所说的:

select 
  user0_.userName as col_0_0_, 
  count(post2_.id) as col_1_0_ 
from User user0_ 
left outer join User_Post posts1_ 
  on user0_.id=posts1_.User_id 
left outer join Post post2_ 
  on posts1_.posts_id=post2_.id 
group 
  by user0_.userName

查询是正确的,但关系定义存在问题。以下是我所拥有的:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    ...         
    @OneToMany
    private List<Post> posts;
    ...
}

@Entity
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    ...
    @ManyToOne
    private User author;
    ...
}

查询是正确的,但关系定义存在问题。以下是我所拥有的:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    ...         
    @OneToMany
    private List<Post> posts;
    ...
}

@Entity
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    ...
    @ManyToOne
    private User author;
    ...
}

猜测一下(很久没有使用Hibernate了):试着在JPQL中去掉
groupby
。@MattBall:SQLGrammarException:-(我不熟悉JPQL,但您没有指定
连接条件
,我的意思是在什么基础上连接表。JPQL使用JPA定义的关联来确定连接条件,因此它不是作为查询的一部分指定的。@Smit:condition由
u.posts
描述-用户之间存在一对多的关系和帖子。只是猜测一下(很长时间没有使用Hibernate):试着在JPQL中去掉
groupby
。@MattBall:SQLGrammarException:-(我不熟悉JPQL,但您没有指定
连接条件
,我的意思是在什么基础上连接表。JPQL使用JPA定义的关联来确定连接条件,因此它不是作为查询的一部分指定的。@Smit:condition由
u.posts
描述-用户之间存在一对多的关系和职位。
    @OneToMany(mappedBy = "author")
    private List<Post> posts;