如何为多对多映射的第三个表编写自定义查询
我创建了具有自连接多对多关系的朋友表。朋友实体具有朋友列表,其结果是用于表示多对多关系的第三个表。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用
@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