0

I have a case, when from request I get a BookDTO with author id, and I have to create and save new book to database with nested Author entity. Now I get author object from database before creating book, but I am not sure that this is a right way. What if I with have multiple nested entity inside one. I have to get them all from DB before save it, or there is a more clean and fast way to do it?


import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;

import javax.persistence.*;

@Entity
@Getter
@Setter
class Book
{

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

        private String name;

        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "author_id")
        private Author author;

        public Book(BookDTO bookDTO, Author author)
        {
                this.setName(bookDTO.getName());
                this.setAuthor(author);
        }
}

@Getter
@Setter
class BookDTO
{

        private String name;

        private Long author_id;
}

@Entity
class Author
{

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

        private String name;
}

@Repository
interface BookRepo extends JpaRepository<Book, Long>
{

}

@Repository
interface AuthorRepo extends JpaRepository<Author, Long>
{

}

@Controller
class BookController
{

        @Autowired
        AuthorRepo authorRepo;

        @Autowired
        BookRepo bookRepo;

        @MutationMapping
        public Book createUser(BookDTO bookDTO)
        {
                // Getting author from DB before create book
                Author author = authorRepo.getReferenceById(bookDTO.getAuthor_id());
                Book book = new Book(bookDTO, author);
                return bookRepo.save(book);
        }
}

How can I save author ID for book without getting Author from database?

Mihail HRS
  • 115
  • 1
  • 1
  • 9
  • Does this answer your question? [Difference between FetchType LAZY and EAGER in Java Persistence API?](https://stackoverflow.com/questions/2990799/difference-between-fetchtype-lazy-and-eager-in-java-persistence-api) – Federico klez Culloca Oct 18 '22 at 10:45
  • @Federico klez Culloca No, In this case it happens when i get entity from db, but in my case I want to get value then create instance of class in java – Mihail HRS Oct 18 '22 at 10:52
  • getReferenceById() doesn't "get (select) it from database"! ... But: "Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is implemented this is very likely to always return an instance and throw an EntityNotFoundException on first access. Some of them will reject invalid identifiers immediately." – xerx593 Oct 18 '22 at 11:22
  • So, you would typically "try, catch" the `save` statement..and have to decide..(how to proceed with unknown author_id) – xerx593 Oct 18 '22 at 11:25
  • hope this will help you https://javatute.com/jpa/spring-data-jpa-nested-property-query-method/ – Abhishek Kumar Jena Oct 18 '22 at 11:46

0 Answers0