使用某些数据库在Java ee中进行查询

时间:2019-01-15 00:16:17

标签: sql java-ee

我正在尝试获取特定学生购买的所有电影,但是在编写查询时遇到问题。

我有这些错误:

  

STUDENT_MOVIEBOUGHT未知

     

l.student_login无法解析为有效类型

     

l.movi​​ebought_id在FORM子句中未定义。

所以我不知道如何编写查询,您能解释一下错误或有关如何编写代码的任何提示吗(对不起,初学者)?

我试图做:

public boolean checkBought(Movie movie, Student student){
    long mid=movie.getId();
    TypedQuery<Long> query=em.createQuery("SELECT l.moviebought_id FROM 
    STUDENT_MOVIEBOUGHT l WHERE l.student_login LIKE '%"+student+"%' AND 
    student_id LIKE '%"+mid"%",Long.class);

    List <Long> moviesId=query.getResultList();
    return moviesId.indexOf(mid)!=-1;
}

数据库:

STUDENT_MOVIEBOUGHT     STUDENT   MOVIEBOUGHT    
  -student_login          -login     -id            
  -moviebought_id         -name      -movie_id
                                     -order_id

student.java

@Id
private String login;    
@OneToMany(cascade = CascadeType.ALL, mappedBy = "student")
private List<Order> order = new ArrayList();
private List<MovieBought> movies=new ArrayList();

moviebought.java

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private Order order;
@ManyToOne
private Movies movies;

order.java

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private Student student;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval=true, mappedBy = 
"order")
private List<MovieBought> movies= new ArrayList();

0 个答案:

没有答案