简体   繁体   中英

Spring MVC Hibernate Many to Many Relationship get org.hibernate.exception.SQLGrammarException: Unknown column in 'on clause'

I have 3 class which is have a relation like this

     @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;

and here the 2nd class

@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;

and then the third class

@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;

What i want here is getting all of the book from course which is selected by student. So in the DAOImpl my code is like this

 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()

when I executed my application I got this exception

nested exception is org.hibernate.exception.SQLGrammarException: Unknown column 'student9_.id_student' in 'on clause'

What I'm doing wrong here?

Thank you for the answer, sorry for my bad english

Try below code for many-to-many. Also, use variable names instead of column names if you use HQL

@ManyToMany(mappedBy = "course", cascade = {CascadeType.ALL})
@LazyCollection(LazyCollectionOption.FALSE)
@JsonIgnore
private List<ModelStudent> student;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM