我有 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;
我想要的是从学生选择的类(class)中获取所有书籍。所以在 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'
我在这里做错了什么?
谢谢你的回答,抱歉我的英语不好
最佳答案
尝试下面的多对多代码。另外,如果使用 HQL,请使用变量名而不是列名
@ManyToMany(mappedBy = "course", cascade = {CascadeType.ALL})
@LazyCollection(LazyCollectionOption.FALSE)
@JsonIgnore
private List<ModelStudent> student;
关于java - Spring MVC Hibernate多对多关系获取org.hibernate.exception.SQLGrammarException : Unknown column in 'on clause' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23448817/