1

I am trying to implement many-to-one relationship in SpringMvc model class. But i am not able to find any example from which i could able to understand how this work.

return (Student_Course) session.getCurrentSession().createQuery("from Student_Course as sc where c.user.id=?").setInteger(0, user_id).uniqueResult()

In above code i am trying to retreive Student_Course table from database through hibernate query but it is giving me this error.

Error

org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'c.user.id' [from com.sanjay31321.sys.model.Student_Course as sc where c.user.id=?]

Is there any other way i can write this hql query so i can get this table. Please help me.

Course Class :

@Entity @Table (name="Course")
public class Course {
    @Id @Column @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @Column(name="course_name")
    private String name;

    //Setter and Getter
}

Student_Course Class:

@Entity @Table (name="Student_course")
public class Student_Course {

    @Id @Column @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @ManyToOne @JoinColumn(name="user_id")
    private User user;

    @ManyToOne @JoinColumn(name="course_id")
    private Course course;

    //Setter and Getter
}

User Class:

@Entity @Table (name="user")
public class User {

    @Id @Column @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @Column(name="email")  @Email
    private String email;

    @Column(name="password") 
    private String password;

    //Setter and Getter
}

StudentCourseDaoImpl Class

@Repository
public class StudentCourseDaoImpl implements StudentCourseDao{

    @Autowired private SessionFactory session;

    @Override
    public Student_Course getStudentCourseByUserID(int user_id) {
        return (Student_Course) session.getCurrentSession().createQuery("from Student_Course as sc where c.user.id=?").setInteger(0, user_id).uniqueResult();
    }

}

1 Answer 1

2

Your alias name is sc, your column name is user_id, so it should be

"from Student_Course as sc where sc.user_id=?"

instead.

Sign up to request clarification or add additional context in comments.

2 Comments

try to do it like following
It is working..I made a mistake there...Thank you very much friend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.