JPQL 表名未映射

Posted

技术标签:

【中文标题】JPQL 表名未映射【英文标题】:JPQL Table name is not mapped 【发布时间】:2018-06-19 21:50:01 【问题描述】:

我对 Spring Boot 比较陌生,这个问题让我抓狂。我有两个表,用户和事件,它们具有多对多关系。我的目标是获取用户尚未加入且不处于用户家庭状态的所有事件。多对多中间表是系统生成的。用户的模型是 包 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 is not mapped [SELECT e FROM Users_events ue JOIN Events e WHERE ue.user_id ?1 AND e.state ?2 ]

谁能解释一下这个问题。谢谢

【问题讨论】:

Users_events 是一个连接表,而不是 entity,因此您无法从中进行选择。 select e from Events e join e.users eu where eu.id &lt;&gt; ? 或类似的东西。 【参考方案1】:

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

在这段代码中

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

改成

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

【讨论】:

以上是关于JPQL 表名未映射的主要内容,如果未能解决你的问题,请参考以下文章

将 SQL(不是 JPQL)映射到简单 Java 对象的集合?

JPQL 构造函数表达式 - org.hibernate.hql.ast.QuerySyntaxException:表未映射

thinkphp 驼峰怎么映射数据库表名

hibernate映射postgreSQL数据库中的表时,表名是大写的时候为啥hibernate不能映射实体

MyBatisPlus:DQL编程控制(条件查询方式查询投影查询条件设定字段映射与表名映射)

Mybatis Generator 未将表名映射到驼峰式大小写