0

I have a ClassRoom class, in my Spring Boot application. Here is what it looks like :

public class ClassRoom {

@Id @GeneratedValue Long classRoomID;

ArrayList<User>adminList=new ArrayList<>();
}

And in my ClassRoomRepository class, I have :

public interface ClassRoomRepository extends JpaRepository<ClassRoom,Long> {

@Query("select ClassRoom from ClassRoom c where c.adminList = ?1")
ArrayList<ClassRoom> findByAdminList(ArrayList<User> adminList);
/*
@Query("select ClassRoom from ClassRoom c where c. = ?1")
ArrayList<ClassRoom> findByAdmin(User admin);
*/
}

I can query to select ClassRoom where ArrayList of ClassRoom gets passed parameter.

But I want to query to select ClassRoom where I pass only one User as parameter and returns ArrayList of ClassRoom.(Commented section-nothing done so far)

If it is possible in this interface, how can I do so?

2 Answers 2

1

Asuming you habe many-to-many association using join table, this is what you need,

 @Query(value = "SELECT DISTINCT cr FROM ClassRoom cr JOIN cr.admins a WHERE a = ?1")
 List<ClassRoom> findAllByAdmin(final Admin admin);
Sign up to request clarification or add additional context in comments.

1 Comment

can I have your mail or some contact, actually I need to discuss with you on this and this too.
1

When mapping collection , Hibernate requires it to declare it using interface type such as List , Set , Map etc. But you are using ArrayList to declare the admin list , I am guessing the whole list will be stored to a single column with some binary data type in DB . No one would store the data in this funny way when using RDBMS unless you have strong reason to do it.

From what you describe , ClassRoom and User seem to be many to many. For demonstration convenience , I would map it as @ManyToMany:

@Entity
@Table
public class ClassRoom {

    @ManyToMany
    @JoinTable(name ="classroom_user",
             joinColumns = @JoinColumn(name = "classroom_id"),
            inverseJoinColumns = @JoinColumn(name = "user_id"))
    private List<User> admins = new ArrayList();
}
@Entity
@Table
public class User{

        @ManyToMany(mappedBy = "admins")
        private List<ClassRoom> classRooms  = Lists.newArrayList();
}

Then given an user admin ,to get all of his administrative class rooms :

@Query("select distinct cr from ClassRoom cr left join fetch cr.admins admin where admin = ?1 ")
public List<ClassRoom> findByAdmin(User admin); 

Or :

@Query("select distinct cr from ClassRoom cr where ?1 member of cr.admins")
public List<ClassRoom> findByAdmin(User admin); 

2 Comments

In the User class, shouldn't it be @ManyToMany(mappedBy = "user") according to this , instead of admins
Hi Maifee, No it should be the property name in the ClassRoom , so it is admins in my example .

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.