0

I have two objects, Student and Book. Student has @OneToMany relationship with Book, and Book has only id, name, and publishYear. What i want is to return from Query is this:

select s.name, 
       b.name, 
       b.publishYear 
from Student s 
     inner join Book b on s.id = b.studentId

How to return s.name, b.name and b.publishYear with map, so i don't want to return a List but a map. How to do that?

1 Answer 1

1

I am assuming you have a method findAll() which returns a list of with StudentBook objects that have properties student name, book name and book publish year.

Now to convert this method to return a map that has as key the book name and as values are book name and book published year to follow the code below:

        Map<String, Book> map = studentBookList.stream()
         .collect(Collectors.toMap(
                        StudentBook::getSname(), 
                        new Book(StudentBook::getBname(),
                                StudentBook::getPublishYear()
                                )
                        )
                );
Sign up to request clarification or add additional context in comments.

Comments

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.