0

I am trying to get data from multiple tables in Spring Data JPA.

I have:

Professor.java

@Entity
public class Professor {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;
  private String username;

    private String password;

    private String firstname;

    private String lastname;

//Generated getters and setters

  @ManyToMany(mappedBy = "professors", fetch = FetchType.EAGER, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
    private List<Classes> classes;

Classes.java

@Entity
public class Classes {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;

    private String name;

    @Lob
    private String description;

 @ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
    private List<Professor> professors;

These entities generate table named as 'classes_professor' and fields as following classes_id and professor_id

I am trying to display professor with some ID that has been given class with some ID.

ClassesRepository.java

@Repository
public interface ClassesRepository extends CrudRepository<Classes,Long>{

    @Query("SELECT p FROM Professor p join p.Classes c WHERE c.id =:id")
    public List<Professor> findProfessorNameById(@Param("id") long id);
}

Controller.java

public List<Professor> findProfessorNameById(@Param("id") long id){
        return classesRepository.findProfessorNameById(id);
    }

When I try to run it I have this error:

could not resolve property: Classes of: com.oggi.model.Professor [SELECT p FROM com.oggi.model.Professor p join p.Classes c WHERE c.id =:id]
Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
  • 1
    `mappedBy = "profesors",... ` seems to be a typo - also in `SELECT p FROM Profesor p join...` and `Classes` (vs `classes` as pointed out by @Matt's answer) – blurfus Aug 14 '17 at 14:18

1 Answers1

2

The error lies in p.Classes. It should rather be p.classes since the attribute in your Professor POJO is written in lowercase.

The Query checks the attributes of the Professor POJO / Entity; it does not look for other Entity names.

On top: Sometimes your are writing "profesor" other times "professor".

Matt
  • 116
  • 6
  • Yes, that was it my error is removed now. I think my query is not working fine because I get list of all professors that I have in table and I should get only one. Do you know why? @Matt –  Aug 14 '17 at 14:54
  • @dome Try this query `SELECT p FROM Professor p WHERE p.classes.id =:id`. – Matt Aug 14 '17 at 15:12
  • 1
    @Matt The query that you mentioned will throw `org.hibernate.QueryException: illegal attempt to dereference collection` exception because `classes` is a collection which you are trying to compare to one id. – Dhaval Simaria Jan 26 '18 at 12:52
  • You are right of course. I don't know what I've been thinking! It probably is no help to the Questioner anymore, but maybe the problem lies in the id generation. The `Professor` and `Classes` entities seem to use the table-strategy for id-generation, but I don't see any declared `TableGenerator`. Anyone running into that issue could [take a look at this answer](https://stackoverflow.com/a/12121914/5589447). – Matt Feb 16 '18 at 21:25