Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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 Spring数据多个选择查询_Java_Spring_Jpa_Spring Data_Spring Data Jpa - Fatal编程技术网

Java Spring数据多个选择查询

Java Spring数据多个选择查询,java,spring,jpa,spring-data,spring-data-jpa,Java,Spring,Jpa,Spring Data,Spring Data Jpa,我有两个实体:用户和事件。每个映射到适当的表。我还有第三个表user\u event,因为这两个实体有多对多的关系。我需要从数据库中选择用户参与的所有事件 活动: @Entity @Table(name = "event") public class Event extends AbstractPersistable<Long> { @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinTable(n

我有两个实体:用户和事件。每个映射到适当的表。我还有第三个表
user\u event
,因为这两个实体有多对多的关系。我需要从数据库中选择用户参与的所有事件

活动:

@Entity
@Table(name = "event")
public class Event extends AbstractPersistable<Long> {

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "user_event",
        joinColumns = @JoinColumn(name = "event_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private Collection<User> participants;
但此查询在应用程序启动时导致异常:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [select e from Event e join user_event ue on ue.event_id = e.id where ue.user_id = :userId and e.startDate > CURRENT_TIMESTAMP]
在MySQL Workbench中,我尝试了以下方法:

select * from event e join user_event ue on e.id = ue.event_id where ue.user_id = 1 and e.startDate > now();
它是有效的。但是如何为spring数据创建良好的工作查询呢

SQL转储:

select count(event0_.id) as col_0_0_ from event event0_ inner join   address address1_ on event0_.address_id=address1_.id 
cross join user_event participan2_, user user3_ where   event0_.id=participan2_.event_id and participan2_.user_id=user3_.id 
and (? in (.)) and event0_.startDate>CURRENT_TIMESTAMP
  • 在@ManyToMany映射中,您有以下内容:
  • @JoinTable(name=“事件\用户\事件”

    但是在查询中您使用的是
    user\u event

  • 在你的询问中

    select e 
    from Event e join user_event ue on ue.event_id = e.id 
    where ue.user_id = :userId and e.startDate > CURRENT_TIMESTAMP"
    
  • 您使用的是
    user\u event
    ,它不是一个实体(在异常消息中正确指出了该实体)。因此查询应如下所示:

    select e 
    from Event e join e.participants u 
    where u.id = :userId and e.startDate > CURRENT_TIMESTAMP
    
    假设您的
    User
    实体具有名为
    id
    的属性,并且此查询应返回与该用户关联的所有事件
    :userId

  • 在@ManyToMany映射中,您有以下内容:
  • @JoinTable(name=“事件\用户\事件”

    但是在查询中您使用的是
    user\u event

  • 在你的询问中

    select e 
    from Event e join user_event ue on ue.event_id = e.id 
    where ue.user_id = :userId and e.startDate > CURRENT_TIMESTAMP"
    
  • 您使用的是
    user\u event
    ,它不是一个实体(在异常消息中正确指出了该实体)。因此查询应如下所示:

    select e 
    from Event e join e.participants u 
    where u.id = :userId and e.startDate > CURRENT_TIMESTAMP
    

    假设您的
    User
    实体有一个名为
    id
    的属性,并且该查询应该返回与该用户相关的所有事件
    :userId

    ,您没有利用JPA的映射。我很确定Spring数据可以用一个简单的存储库名称来表达您的查询,但即使在JPQL中,您也应该只使用need
    从事件e中选择e,其中:事件中的用户。参与者
    (并传递
    用户
    对象而不是ID)。@chrylis,它不起作用-从事件e中选择e,其中:事件中的用户(事件参与者)打字错误。请尝试
    从事件e中选择e,其中:e.participants中的user
    @chrylis,相同的错误com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误;请查看与您的mysql服务器版本对应的手册,以了解要使用的正确语法。)您能否将JPA提供程序设置为转储它生成的SQL并发布堆栈跟踪?这看起来有点问题。你没有利用JPA的映射。我很确定Spring数据可以用一个简单的存储库名称来表达您的查询,但即使在JPQL中,您也只需要
    从事件e中选择e,其中:事件中的用户。参与者
    (并传递
    用户
    对象而不是ID)。@chrylis,它不起作用-从事件e中选择e,其中:事件中的用户。尝试
    从事件e中选择e,其中:e.particients中的user
    @chrylis,相同的错误com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以获得正确的语法使用方法(near'))您可以将JPA提供程序设置为转储它生成的SQL并发布堆栈跟踪吗?这里好像有什么故障。