Spring JPA。仅调用@ManyToMany,而不是整个对象。

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

Spring JPA. Call @ManyToMany ONLY and not the whole object

问题

假设我有类似这样的代码:

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "assigned_tasks", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "task_id", referencedColumnName = "id"))
private Set<Task> assignedTasksDoer = new HashSet<>();

在我的 "User" 实体类中。

当我获取用户时,它会将所有详细信息都带给我。这非常好,没有任何错误。
但是我如何将其拆分成多个部分,例如,当我只想从与 UserController 不同的控制器(例如 AssignedTasksController)中获取“assigned_tasks”时,我该如何做呢?

如果有人感兴趣,我愿意提供更多细节。

由于我刚开始使用面向对象的方法,请帮助我!是的,使用 JPA、实体、DAO 和存储库非常好,让 Spring 在幕后处理所有事情也很棒,但是否有局限性呢?我是否可以完全避免编写 SQL 查询和准备语句,或者在其他必须使用它们的情况下采取其他方法呢?

简而言之:User 实体还有其他字段。我希望创建一个控制器路由,仅每次获取用户的分配任务,而不是整个对象。我该如何做到这一点?

英文:

Suppose I have something like this:

   @ManyToMany(fetch = FetchType.LAZY)
   @JoinTable(name = &quot;assigned_tasks&quot;, joinColumns = @JoinColumn(name = &quot;user_id&quot;, referencedColumnName = &quot;id&quot;), inverseJoinColumns = @JoinColumn(name = &quot;task_id&quot;, referencedColumnName = &quot;id&quot;))
   private Set&lt;Task&gt; assignedTasksDoer = new HashSet&lt;&gt;();

in my "User" entity class.

When I fetch the user it brings me all the details. And that's great. No errors whatsoever.
But how can I break it apart in pieces, so for example, when I want to fetch ONLY the "assigned_tasks" OF the User I can just fetch it from a different controller other than the UserController, i.e. the AssignedTasksController?

I'm willing to provide more details if anyone is interested.

Please help me as I am starting this Object Oriented approach!! Yes, working with jpa, entities, DAOs and repositories is great, while letting Spring take care of everything under the hood, is great, but are there limitations to it? Can I completely avoid writing sql queries and prepares statements or other cases where I will have to resort to it?

TL;DR: The User entity has other fields too. I want make a controller route that only fetches the assigned tasks of the user, and not the whole object everytime. How can I do that?

答案1

得分: 1

我假设你正在使用Spring Data JPA。在这种情况下,你很可能已经有类似于UserRepository extends JpaRepository<User, Long>(假设主键是Long类型)的东西。

只需创建一个TaskRepository extends JpaRepository<Task, Long>,并添加以下方法:List<Task> findAllByUserId(Long userId); 到该接口中。这假设Task有一个字段User user,而User的主键字段称为id(因此有"byUserId")。

有关如何使用Spring Data仓库创建查询的更多详细信息,请参见Spring Data JPA,查询创建

如果需要更多控制,还可以查看同一文档中的@Query注解。

英文:

I assume you are using Spring Data JPA. In that case you most likely already have something like a UserRepository extends JpaRepositry&lt;User, Long&gt; (assuming the primary key is a Long).

Just create a TaskRepository extends JpaRepository&lt;Task, Long&gt; and add the following method: List&lt;Task&gt; findAllByUserId(Long userId); to that interface. This assumes that Task has a field User user and the primary field of User is called id (hence the "byUserId").

See Spring Data JPA, Query Creation for more details on how to create queries using a Spring Data repository.

If you need more control, you can also take a look at the @Query annotation in the same documentation.

huangapple
  • 本文由 发表于 2020年8月15日 13:20:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63422851.html
匿名

发表评论

匿名网友

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

确定