Spring Data Rest中如何实现@ManyToMany双向关联
目前,我正在学习 Spring Data Rest,其中我有两个实体“应用程序”和“应用程序”。 “组件”,其中两者之间存在使用 Spring Data Rest 构建的“@ManyToMany”关联。
应用程序实体
@Entity
@Table(name = "application")
@Getter
@Setter
@ToString
public class Application {
---
---
@ManyToMany(mappedBy = "applications", cascade = CascadeType.PERSIST)
@JsonIgnore
private List<Component> components = new ArrayList<Component>();
//Helper methods
public void addComponent(Component component) {
components.add(component);
component.getApplications().add(this);
}
public void removeComponent(Component component) {
components.remove(component);
component.getApplications().remove(this);
}
}
组件实体
@Entity
@Table(name = "component")
@Getter
@Setter
@ToString
public class Component{
---
---
@ManyToMany(cascade = CascadeType.PERSIST)
@JoinTable(name = "component_application",
joinColumns = @JoinColumn(name = "component_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "application_id",referencedColumnName = "id"))
@JsonIgnore
private List<Application> applications = new ArrayList<Application>();
}
在测试关联的 API 时,与组件实体(所有者类)关联的 API 正在工作 组件关联的 API
curl -X PUT "https://ENVIRONMENT.com/components/1/applications" -H "accept: */*" -H "Content-Type: text/uri-list" -d BODY_LIST
与主体
https://ENVIRONMENT.com/applications/1
https://ENVIRONMENT.com/applications/12
但是当我尝试从应用程序实体(托管类)创建双向关联时,关联 API 不起作用,PUT API 调用得到 204,但当 GET 调用时没有任何关联完毕。 应用程序关联 API
curl -X PUT "https://ENVIRONMENT.com/applications/1/component" -H "accept: */*" -H "Content-Type: text/uri-list" -d BODY_LIST
与主体
https://ENVIRONMENT.com/components/1
https://ENVIRONMENT.com/components/2
我尝试在托管类(此处为“应用程序实体”)中添加自定义添加、删除方法,但仍然存在相同的问题。请让我知道我错过了什么。
Currently, I'm learning Spring Data Rest, where I am having two Entities "Application" & "Component" where both are having "@ManyToMany" associations present between them built using Spring Data Rest.
Application Entity
@Entity
@Table(name = "application")
@Getter
@Setter
@ToString
public class Application {
---
---
@ManyToMany(mappedBy = "applications", cascade = CascadeType.PERSIST)
@JsonIgnore
private List<Component> components = new ArrayList<Component>();
//Helper methods
public void addComponent(Component component) {
components.add(component);
component.getApplications().add(this);
}
public void removeComponent(Component component) {
components.remove(component);
component.getApplications().remove(this);
}
}
Component Entity
@Entity
@Table(name = "component")
@Getter
@Setter
@ToString
public class Component{
---
---
@ManyToMany(cascade = CascadeType.PERSIST)
@JoinTable(name = "component_application",
joinColumns = @JoinColumn(name = "component_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "application_id",referencedColumnName = "id"))
@JsonIgnore
private List<Application> applications = new ArrayList<Application>();
}
while testing associated APIs, APIs associated with Component Entity (owner class) is working
Component associated API
curl -X PUT "https://ENVIRONMENT.com/components/1/applications" -H "accept: */*" -H "Content-Type: text/uri-list" -d BODY_LIST
with body
https://ENVIRONMENT.com/applications/1
https://ENVIRONMENT.com/applications/12
But when I tried to create bi-directional associations from Application Entity (managed class) association APIs are not working, getting 204 for the PUT API call but nothing got associated when GET call is done.
Application association API
curl -X PUT "https://ENVIRONMENT.com/applications/1/component" -H "accept: */*" -H "Content-Type: text/uri-list" -d BODY_LIST
with body
https://ENVIRONMENT.com/components/1
https://ENVIRONMENT.com/components/2
I tried adding custom add, remove methods in a managed class which is "Application Entity" here, still the same issue. Please let me know where I'm missing out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1 个实体:
2 个实体:
这就是定义双向多对多关联所需要做的全部工作。现在,您可以在 JPQL 或 Criteria API 查询中或在域对象上双向导航。
双向关联在查询中很容易使用,但在持久保存新实体时还需要执行额外的步骤。
当您添加或删除实体时,您需要更新双方的关联。
您可以在以下代码片段中看到它的示例,其中我首先创建一个新的作者实体并将图书实体添加到图书列表中。
之后,我还需要将新的作者实体添加到图书实体的作者列表中。
更新两个实体上的关联是一项容易出错的任务。因此,为其提供辅助方法是一个很好的做法。
请参阅:https://thorben-janssen.com/hibernate -tips-map-双向-多多关联/
1 Entity:
2 Entity:
That’s all you need to do to define a bidirectional many-to-many association. You can now navigate it in both directions in your JPQL or Criteria API queries or on your domain objects.
Bidirectional associations are easy to use in queries, but they also require an additional step when you persist a new entity.
You need to update the association on both sides when you add or remove an entity.
You can see an example of it in the following code snippet in which I first create a new Author entity and add the Book entity to the List of books.
And after that, I also need to add the new Author entity to the List of authors on the Book entity.
Updating the associations on both entities is an error-prone task. It’s, therefore, a good practice to provide helper methods for it.
see: https://thorben-janssen.com/hibernate-tips-map-bidirectional-many-many-association/