Search code examples
javamysqlsqlhibernatehql

get value tables in hibernate


I have a project with to table (Student and Course) and a relational table StudentCourse :

@ManyToMany(cascade = CascadeType.ALL )
@JoinTable(
        name = "StudentCourse",
        joinColumns = { @JoinColumn(name = "studentid") },
        inverseJoinColumns = { @JoinColumn(name = "courseid") }
)

now I want to give value of this table and join it to another table . but it dosen't work . help me please. and i want to know is that any other way better than this work that i do .....this is my code for this work :

 public static List<Course> getSelectedCurse() throws SQLException {
   yourstcode = Console.getInputInteger("enter your code");
   Session session = OpenSession1();
   List<Course> courses = session.createNamedQuery("SELECT coursename FROM Course where id in (SELECT courseid from StudentCourse WHERE studentid=" + yourstcode + "").list();
   CloseSession(session);
   return courses;
}

//courseid and coursecode are my foreign key that refer to id in Course and Student table


Solution

  • I think it's not working because you are using createNamedQuery method. use createQuery if you are using JPQL syntax or createNativeQuery if you want to use pure sql syntax.