Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9042297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:27:12+00:00 2026-06-16T10:27:12+00:00

Supposing I have entity Lecturer and Course two entities having many to many relationships

  • 0

Supposing I have entity Lecturer and Course two entities having many to many relationships in JPA 2.0

It is perfectly OK to have a non-annotated entity LecturerCourse (with multi-table selection columns, not annotated) and JPQL like this working.

        final Query query = em.createQuery(
            "select new com.cs.entity.LectureCourse(l.id, l.name, l.surName, " +
                    "l.type, c.code, c.name) from lecturer l join l.courses c where l.id = :l_id"
    );

How can I do the same with JPA Criteria API?

Here are the code for Lecturer and Courses respectively

@Entity(name = "course")
public class Course implements Serializable {

    @Id
    @Column(name = "course_code", columnDefinition = "char(6)",
            unique = true, nullable = false)
    private String code;
    @Column(name = "course_name")
    private String name;
    @ManyToMany(mappedBy = "courseSet", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<Student> students;
    @ManyToOne(fetch = FetchType.EAGER, targetEntity = Lecturer.class)
    @JoinColumn(name = "lecture_id")
    private Lecturer lecturer;

    public Course() {
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Student> getStudents() {
        return students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    }

    public Lecturer getLecturer() {
        return lecturer;
    }

    public void setLecturer(Lecturer lecturer) {
        this.lecturer = lecturer;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Course)) return false;

        Course course = (Course) o;

        if (code != null ? !code.equals(course.code) : course.code != null) return false;
        if (name != null ? !name.equals(course.name) : course.name != null) return false;
        if (students != null ? !students.equals(course.students) : course.students != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = code != null ? code.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (students != null ? students.hashCode() : 0);
        return result;
    }
}


@Entity(name = "lecturer")
public class Lecturer {

    public static enum LectureType{
        FULL_TIME, PART_TIME
    }

    @Id
    @Column(name = "lecture_id", nullable = false,
            unique = true, columnDefinition = "char(8)")
    private String id;
    @Column(name = "lecture_name", length = 20, nullable = false)
    private String name;
    @Column(name = "lecture_sur_name", length = 20, nullable = false)
    private String surName;
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "lecture_type", columnDefinition = "char(1)")
    private LectureType type;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "lecturer", fetch = FetchType.EAGER)
    private Set<Course> courses;

    public Lecturer() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurName() {
        return surName;
    }

    public void setSurName(String surName) {
        this.surName = surName;
    }

    public LectureType getType() {
        return type;
    }

    public void setType(LectureType type) {
        this.type = type;
    }

    public Set<Course> getCourses() {
        return courses;
    }

    public void setCourses(Set<Course> courses) {
        this.courses = courses;
        for(final Course course : this.courses){
            course.setLecturer(this);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Lecturer)) return false;

        Lecturer lecturer = (Lecturer) o;

        if (id != null ? !id.equals(lecturer.id) : lecturer.id != null) return false;
        if (name != null ? !name.equals(lecturer.name) : lecturer.name != null) return false;
        if (surName != null ? !surName.equals(lecturer.surName) : lecturer.surName != null) return false;
        if (type != lecturer.type) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (surName != null ? surName.hashCode() : 0);
        result = 31 * result + (type != null ? type.hashCode() : 0);
        return result;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:27:13+00:00Added an answer on June 16, 2026 at 10:27 am

    What you’re looking for is a form of CriteriaQuery#multiselect … I’m assuming you’re generating an appropriate static metamodel (that’s where the EntityClass_.field pieces come from) but working by hand I come up with something like:

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<LectureCourse> criteria = builder.createQuery(LectureCourse.class);
    Root<Lecturer> l = criteria.from(Lecturer.class);
    Path<Course> c = l.join(Lecturer_.courses);
    ParameterExpression<String> l_id = builder.parameter(String.class);
    Predicate lecturerIdMatches = builder.equal(l.get(Lecturer_.id),l_id);
    TypedQuery<LectureCourse> query = em.createQuery(criteria.multiselect(l.get(Lecturer_.id), l.get(Lecturer_.name), l.get(Lecturer_.surName), l.get(Lecturer_.type), c.get(Course_.code), c.get(Course_.name)).where(lecturerIdMatches));
    
    query.setParameter(l_id,"queryvalue");
    List<LectureCourse> results = query.getResultList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposing I have two entities a parent object and a child. I want the
Supposing I have two classes, Customer and Order, where one Customer can have one-or-many
MVC newbie question re binders. Supposing I have two strongly typed partial actions that
Supposing I have two different languages for Localizable.strings and a single target. There's any
Supposing you have a 3d box of cubes, with each cube having 3 indices:
I have two entites: Folder and Picture . A Folder can have many Pictures
Supposing I have a StringerHelper(Component|Helper|Behaviour).php class with a method for random strings. I might
Supposing I have 3 tables: Users, Roles and UserRoles. I can fetch the user_roles
Supposing I have one perl in /usr/bin (which came along with my os-distribution) and
Supposing I have this: foo bar 1 and foo bar 2 How can I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.