简体   繁体   English

ManyToOne FetchType.LAZY 不起作用。 null 获取列表时总是返回

[英]ManyToOne FetchType.LAZY does not work. null always returned when getting the list

I am using JPA with Spring Boot.我正在使用 JPA 和 Spring 引导。 I have entities:我有实体:

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(nullable = false, unique = true)
    private String title;

    @Column(nullable = false)
    private String author;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "bookshelf_id", referencedColumnName = "id", nullable = true)
    private Bookshelf bookshelf;

    // getter & setter
}

@Entity
public class Bookshelf {
    @Id
    @GeneratedValue
    private long id;

    @Column(nullable = false)
    private String location;

    @OneToMany(mappedBy = "bookshelf", fetch = FetchType.LAZY)
    private List<Book> books;

    // getter & setter
}

Then in my test, I try to retrieve the book placed in the bookshelf:然后在我的测试中,我尝试检索放在书架上的书:

        Bookshelf shelf = new Bookshelf("room A");
        Book book = new Book("a book", "chris");
        book.setBookshelf(shelf);;
        entityManager.persist(book);
        entityManager.persist(shelf);
        entityManager.flush();

        Bookshelf persistedShelf = bookshelfRepository.findById(shelf.getId()).get();
        Book persistedBook = bookRepository.findById(book.getId()).get();

        book = null;
        shelf = null;

        shelf = persistedBook.getBookshelf();
        int count = persistedShelf.getBooks().size();
        assertEquals(1, count);

The test fails because getBooks() always return null in this line:测试失败,因为 getBooks() 总是在此行中返回 null:

        int count = persistedShelf.getBooks().size();

Test log shows only two SQL commands are executed:测试日志显示只执行了两条 SQL 命令:

Hibernate: insert into bookshelf (location, id) values (?, ?)
Hibernate: insert into book (author, bookshelf_id, title, id) values (?, ?, ?, ?)

What can be the problem?可能是什么问题?

Try also setting the inverse relationship from shelf to book:尝试设置从书架到书的反比关系:

Bookshelf shelf = new Bookshelf("room A");
Book book = new Book("a book", "chris");
book.setBookshelf(shelf);

// new code here:
List<Book> books = new ArrayList<>();
books.add(book);
shelf.setBooks(books);

entityManager.persist(book);
entityManager.persist(shelf);
entityManager.flush();

In general you are responsible for managing both sides of the relationship in JPA/Hibernate.一般来说,您负责管理 JPA/Hibernate 中关系的双方。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在一个Set上使用FetchType.LAZY而不在另一个Set上使用LazyInitializationException? - Why am I getting a LazyInitializationException when using FetchType.LAZY on one Set, but not on the other? FetchType.LAZY 作为 FetchType.EAGER - FetchType.LAZY is working as FetchType.EAGER 为什么 Spring MVC controller 方法会忽略 FetchType.LAZY 行为并像 FetchType.EAGER 一样工作? - Why does Spring MVC controller method ignore FetchType.LAZY behaviour and act like FetchType.EAGER? FetchType.LAZY不显示外键列 - FetchType.LAZY not show the Foreign Key Column Hibernate @OneToOne(fetch = FetchType.LAZY)无效 - Hibernate @OneToOne(fetch = FetchType.LAZY) is not working FetchType.LAZY 不适用于@OneToOne 关系 - FetchType.LAZY not working for @OneToOne relation FetchType.Lazy 在 spring 引导中的 JPA 中不起作用 - FetchType.Lazy not working in JPA in spring boot 如何将 FetchType.LAZY 与 ManyToMany 一起使用? - How to use FetchType.LAZY with ManyToMany? Spring Data JPA,Hibernate,@ ManyToOne(fetch = FetchType.LAZY)和org.hibernate.LazyInitializationException:无法初始化代理-没有会话 - Spring Data JPA, Hibernate, @ManyToOne(fetch=FetchType.LAZY) and org.hibernate.LazyInitializationException: could not initialize proxy - no Session Hibernate @LazyToOne(LazyToOneOption.NO_PROXY) @OneToOne(fetch = FetchType.LAZY) 总是急切地获取 - Hibernate @LazyToOne(LazyToOneOption.NO_PROXY) @OneToOne(fetch = FetchType.LAZY) always fetched eagerly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM