我有3个类,它有这样的关系
@Entity
@Table(name="tbl_mhs")
public class ModelStudent {
@Id
@GeneratedValue
@Type(type="java.lang.Integer")
@Column(name="id_student")
private Integer id;
@ManyToMany(cascade = {CascadeType.ALL})
@LazyCollection(LazyCollectionOption.FALSE)
@JsonIgnore
@JoinTable(name="tbl_course_selected",
joinColumns={@JoinColumn(name="id_student")},
inverseJoinColumns={@JoinColumn(name="id_course")})
private List<ModelCourse> course;
这里是第二课
@Entity
@Table(name="tbl_makul")
public class ModelCourse {
@Id
@GeneratedValue
@Type(type="java.lang.Integer")
@Column(name="id_course")
private Integer id;
@ManyToMany(cascade = {CascadeType.ALL})
@LazyCollection(LazyCollectionOption.FALSE)
@JsonIgnore
@JoinTable(name="tbl_course_selectedl",
joinColumns={@JoinColumn(name="id_course")},
inverseJoinColumns={@JoinColumn(name="id_student")})
private List<ModelStudent> student;
然后是第三节
@Entity
@Table(name="tbl_materi")
public class ModelBook {
@Id
@GeneratedValue
@Type(type="java.lang.Integer")
@Column(name="id_book")
private Integer id;
@ManyToOne
@JoinColumn(name="id_course")
private ModelCourse course;
我想要的是从学生选择的课程中获取所有书籍。所以在DAOImpl中我的代码就像这样
List<ModelBook> books = new ArrayList<ModelBook>();
Criteria criteria = getCurrentSession().createCriteria(ModelBook.class);
criteria.createAlias("course", "courseAlias");
criteria.createAlias("courseAlias.student", "studentAlias");
criteria.add(Restrictions.eq("studentAlias.id", student_id));
return criteria.list()
当我执行我的应用程序时,我遇到了这个异常
nested exception is org.hibernate.exception.SQLGrammarException: Unknown column 'student9_.id_student' in 'on clause'
我在这里做错了什么?
谢谢你的回答,抱歉我的英文不好
答案 0 :(得分:1)
尝试使用以下代码进行多对多操作。此外,如果使用HQL
,请使用变量名而不是列名@ManyToMany(mappedBy = "course", cascade = {CascadeType.ALL})
@LazyCollection(LazyCollectionOption.FALSE)
@JsonIgnore
private List<ModelStudent> student;