不能再加入第二张桌子了

时间:2018-05-19 17:49:22

标签: java sql spring hibernate

我试图离开加入第二张桌子,但它并没有显示出来。它只是给了我所有用户而不是所有用户以及FinishedExams。

这是我的存储库中的方法:

public interface IUserRepository extends CrudRepository<User, Integer> {
    @Query("SELECT u FROM User u LEFT JOIN FinishedExam f ON u.id = f.user")
    List<User> getAllWithExams();
}

在我的FinishedExamController中:

@Autowired
private IFinishedExamRepository finishedExamRepository;

@Autowired
private IUserRepository userRepository;

@GetMapping("/allUsersWithExams")
@Fetch(FetchMode.SELECT)
public Iterable<User> getAllUsersWithTheirExams()
{
    return userRepository.getAllWithExams();
}

我的用户模型:

@Entity
@Table(name = "users")
public class User implements Serializable {
    @Id
    @GeneratedValue()
    @Column(name = "id")
    private int id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "role_id", nullable = false)
    public Role role;

    public String getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(String studentNumber) {
        this.studentNumber = studentNumber;
    }

    @Column(name = "student_number", nullable = true)
    private String studentNumber;

    @Column(name = "first_name", nullable = true)
    private String firstName;

    @Column(name = "last_name", nullable = true)
    private String lastName;

    @Column(name = "email", nullable = true, unique = true)
    private String email;

    @OneToOne(fetch = FetchType.EAGER, mappedBy = "user")
    private FinishedExam finishedExam;

    @JsonIgnore
    @Column(name = "password", nullable = true)
    private String password;


    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    private LocalDateTime updatedAt;


    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "event_users",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "event_id", referencedColumnName = "id")
    )

    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }

    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = createdAt;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public String getRole() {
        return role.getName();
    }

    public void setRole(Role role) {
        this.role = role;
    }
}

完成考试模型:

@Entity
@Table(name = "finished_exams")
public class FinishedExam implements Serializable {
    @Id
    @GeneratedValue()
    @Column(name = "id")
    private int id;

    @OneToOne(fetch = FetchType.EAGER)
    @Fetch(FetchMode.SELECT)
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "exam_id")
    private Exam exam;

    @Column(name = "finishedExam", nullable = false)
    private String finishedExam;

    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = createdAt;
    }


    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }

    public int getId() {
        return id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Exam getExam() {
        return exam;
    }

    public void setExam(Exam exam) {
        this.exam = exam;
    }

    public String getFinishedExam() {
        return finishedExam;
    }

    public void setFinishedExam(String finishedExam) {
        this.finishedExam = finishedExam;
    }

    public void setId(int id) {
        this.id = id;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
}

我可以在ON子句中放入任何我想要的东西,但没有任何改变。

2 个答案:

答案 0 :(得分:1)

您需要提供从您的实体到目标实体加入的路径:

替换查询

@Query("SELECT u FROM User u LEFT JOIN FinishedExam f ON u.id = f.user")

@Query("SELECT u FROM User u LEFT JOIN u.finishedExam f ON u.id = f.user.id")

答案 1 :(得分:1)

  

我可以在ON子句中放入任何我想要的东西,但没有任何改变。

当然没什么变化。您正在执行1:1关系的左连接,选择(仅)左实体,而不在该实体上放置任何过滤条件。连接标准(实际上是连接本身)无关紧要:您的查询等同于SELECT u FROM User u

您是否可能意味着执行内部联接?