在Hibernate中进行一对多映射的查询。

huangapple go评论69阅读模式
英文:

Querying in One to many mappings in hibernate

问题

以下是您要翻译的部分:

我有一个书籍实体和一个图书馆实体一个图书馆可以有多本书但每本书只属于一个图书馆)。它们的定义如下

public class Library
{
    @Id
    private Long id;

    @OneToMany(mappedBy = "library")
    private List<Book> books = new ArrayList<>();
}


public class Book
{
    @Id
    private Long id;

    @ManyToOne
    @JoinColumn(name="FK_Library", nullable=false)
    private Library library;
}

我想要找到属于特定图书馆的所有书籍一种方法是创建一个`LibraryRepository`,如下所示

public interface LibraryRepository extends JpaRepository<Library, Long>{}

现在我有一个`findById(Long libraryId)`方法它返回一个包含`List<Book>``Library`对象

我想知道是否可能创建一个`BookRepository`,并声明一个名为`findByLibraryId(Long libraryId)`的方法并让Hibernate通过外键`FK_Library`查询`Book`**而不执行任何连接操作**

像这样

public interface BookRepository extends JpaRepository<Book, Long> {
    public List<Book> findByLibraryId(Long libraryId);
}

上述方法`findByLibraryId`确实执行连接操作我不希望它执行连接操作因为我只想查找由外键关系定义的所有具有`library_id`的行

另外第一种方法是否比第二种方法更可取
英文:

Let's say I have a book entity and a library entity (one library can have multiple books, but each book belongs to only one library). They are defined as follows:

public class Library
{
    @Id
    private Long id;

    @OneToMany(mappedBy = &quot;library&quot;)
    private List&lt;Book&gt; books = new ArrayList&lt;&gt;();
}

and

public class Book
{
    @Id
    private Long id;

    @ManyToOne
    @JoinColumn(name=&quot;FK_Library&quot;, nullable=false)
    private Library library;
}

I want to find all books which belong to a particular library. One way is to create a LibraryRepository as follows:

public interface LibraryRepository extends JpaRepository&lt;Library, Long&gt;{}

Now I have findById(Long libraryId) method that returns a Library object with a List&lt;Book&gt; in it.

I was wondering if it is possible to create a BookRepository and declare a method named findByLibraryId(Long libraryId) and let hibernate query the Book table by the foreign key FK_Library, without performing any joins.

Something like this:

public interface BookRepository extends JpaRepository&lt;Book, Long&gt; {
    public List&lt;Book&gt; findByLibraryId(Long libraryId);
}

The above method findByLibraryId does perform join. I do not want it to perform a join, since all I want is find all rows which have library_id defined by foreign key relationship.

Also is the first method more preferable then second one?

答案1

得分: 3

你可以使用JPQL的@Query来实现这个功能。

@Query("SELECT b FROM Book b WHERE b.library.id = :libraryId")
public List<Book> findByLibraryId(Long libraryId);
英文:

You can use @Query with JPQL to do that

@Query(&quot;SELECT b FROM Book b WHERE b.library.id = :libraryId&quot;)
public List&lt;Book&gt; findByLibraryId(Long libraryId);

答案2

得分: 2

你可以将@ManyToOne关系更改为懒加载。

@ManyToOne(fetch = FetchType.LAZY)

这是OneToMany的默认设置,这就是为什么在搜索Library时你看不到连接的原因。

英文:

You could change the @ManyToOne relationship to lazy loading.

@ManyToOne(fetch = FetchType.LAZY)

This is the default for OneToMany, thats why you see no join when you search the Library.

huangapple
  • 本文由 发表于 2020年8月10日 18:59:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63338868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定