JPQL表名称未映射

时间:2018-06-19 21:50:02

标签: java spring-boot jpql

我是Spring Boot的一个相对较新的人,这个问题使我发疯。我有两个表,Users和Events具有多对多关系。我的目标是获取用户尚未加入并且不在用户家庭状态的所有事件。多对多中间表是由系统生成的。用户的模型是     包com.starxfighter.events.models;

import java.util.Date;
import java.util.List;

import javax.persistence.*;
import javax.validation.constraints.*;

@Entity
@Table(name="users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Size(min = 2, message="First Name must be longer than two characters")
private String fname;
@Size(min = 2, message="Last Name must be longer than two characters")
private String lname;
@Email(message = "Email must be valid")
private String email;
@Size(min = 2, message="City must be longer than two chracters")
private String city;
@NotNull(message = "State can not be left blank")
private String state;
@Size(min = 5, message="Password must be greated than 5 characters")
private String password;
@Transient
private String passconf;
@Column(updatable=false)
private Date createdAt;
private Date updatedAt;

@OneToMany(mappedBy="host", fetch=FetchType.LAZY)
private List<Event> eventHost;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
        name = "users_events",
        joinColumns = @JoinColumn(name="user_id"),
        inverseJoinColumns = @JoinColumn(name="event_id")
)
private List<Event> events;

事件的模型是     包com.starxfighter.events.models;

import java.util.Date;
import java.util.List;

import javax.persistence.*;
import javax.validation.constraints.*;


@Entity
@Table(name="events")
public class Event {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Size(min = 2, message="Event name must be greater than 2 characters")
private String name;
@Future
private Date eventDate;
@Size(min = 2, message="Location must be longer than 2 characters")
private String location;
@Column(updatable=false)
private Date createdAt;
private Date updatedAt;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name= "user_id")
private User host;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
        name="users_events",
        joinColumns=@JoinColumn(name="event_id"),
        inverseJoinColumns = @JoinColumn(name="user_id")
)
private List<User> users;

@OneToMany(mappedBy="event", fetch=FetchType.LAZY)
private List<Comment> comments;

public Event() {
}

正在进行查询调用的我的存储库是:

import com.starxfighter.events.models.Event;

@Repository
public interface EventRepository extends CrudRepository<Event, Long> {

@Query("SELECT e FROM Users_events ue JOIN Events e WHERE ue.user_id <> ?1    AND e.state <> ?2")
List<Event> getOutOfState(Long id, String state);

@Query("SELECT e FROM Event e WHERE e.host = ?1")
List<Event> eventsHostedByUser(Long id);

@Query("SELECT e FROM Users_Events ue JOIN Events e WHERE ue.user_id = ?1")
List<Event> eventsJoinByUser(Long id);

@Query("SELECT e from Users_Events ue JOIN Events e WHERE ue.user_id <> ?1")
List<Event> eventsNotByUser(Long id);

}

确切的错误消息是:org.hibernate.hql.internal.ast.QuerySyntaxException:未映射Users_events [从Users_events ue JOIN事件中选择ue.user_id <>?1和e.state <>?2 ]

任何人都可以在这个问题上阐明一下。谢谢

1 个答案:

答案 0 :(得分:0)

我认为您只是将mapdBy设置为错误的名称,spring找不到名称“主机”

此代码中

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name= "user_id")
private User host;

更改为

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name= "host") <-- change this line
private User host;