如何为多对多映射的第三个表编写自定义查询

发布于 2025-01-13 22:14:28 字数 1801 浏览 1 评论 0原文

我创建了具有自连接多对多关系的朋友表。朋友实体具有朋友列表,其结果是用于表示多对多关系的第三个表。

public class Friends {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;
    @ManyToMany
    @JoinTable(
              name = "friends_list", 
              joinColumns = @JoinColumn(name = "friends_from"), 
              inverseJoinColumns = @JoinColumn(name = "friends_to"))
    private List<Friends> friends;
    
    
}

我想在连接表上创建一个查询。我已经创建了像现在这样的查询

@Query(value="Select * from friends_list f where f.friends_from=:id", nativeQuery=true)
    public List<Friends> findFriendsById(@Param("id") Long id);

,当我提出创建资源的请求时,它会抛出以下错误。

@RestController
public class FriendsController {
    @Autowired
    private FriendsService friendsService;
    
    @PostMapping("/friends")
    public Friends createFriends(@RequestBody Friends frnd)
    {
        return friendsService.createFriends(frnd);
    }
    
    @GetMapping("/friends/{id}/distance/{k}")
    public List<Friends> getFriendsAtDistanceK(@PathVariable("id") Long id,@PathVariable("k") int k)
    {
        return friendsService.getFriendsAtDistanceK(id, k);
    }
}

请求

{
    "id":1,
    "name":"sujeet",
    "friends":[
        {
            "friends_from":1,
            "friends_to":2
        }
    ]
}

我已经创建了两个朋友,但没有朋友列表。我正在尝试通过朋友列表添加朋友。但它显示以下错误。

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.learn.friends.entity.Friends
    at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:347) ~[hibernate-core-5.6.5.Final.jar:5.6.5.Final]

I have created the Friends table which have self join ManyToMany relation. Friends entity have List of Friends which result as third table for representing the ManyToMany relation.

public class Friends {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;
    @ManyToMany
    @JoinTable(
              name = "friends_list", 
              joinColumns = @JoinColumn(name = "friends_from"), 
              inverseJoinColumns = @JoinColumn(name = "friends_to"))
    private List<Friends> friends;
    
    
}

I want to create a query on joined table. I have created Query like

@Query(value="Select * from friends_list f where f.friends_from=:id", nativeQuery=true)
    public List<Friends> findFriendsById(@Param("id") Long id);

Now, It throws the below error when I raise request of creating the resource.

@RestController
public class FriendsController {
    @Autowired
    private FriendsService friendsService;
    
    @PostMapping("/friends")
    public Friends createFriends(@RequestBody Friends frnd)
    {
        return friendsService.createFriends(frnd);
    }
    
    @GetMapping("/friends/{id}/distance/{k}")
    public List<Friends> getFriendsAtDistanceK(@PathVariable("id") Long id,@PathVariable("k") int k)
    {
        return friendsService.getFriendsAtDistanceK(id, k);
    }
}

Request

{
    "id":1,
    "name":"sujeet",
    "friends":[
        {
            "friends_from":1,
            "friends_to":2
        }
    ]
}

I have already created two friends without list of friends. And I am trying to add friends with list of friends. But it is showing below error.

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.learn.friends.entity.Friends
    at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:347) ~[hibernate-core-5.6.5.Final.jar:5.6.5.Final]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆消瘦 2025-01-20 22:14:28

您是否尝试过使用 @Cascade({ CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.PERSIST}) 注释来实现此目的?此解决方案的来源是此处

Have you tried using @Cascade({ CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.PERSIST}) annotation for this? The source to this solution is here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文