Spring Data Rest中如何实现@ManyToMany双向关联

发布于 2025-01-10 15:45:10 字数 2050 浏览 0 评论 0原文

目前,我正在学习 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 技术交流群。

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

发布评论

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

评论(1

雨的味道风的声音 2025-01-17 15:45:10

1 个实体:

@Entity
public class Book {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
 
    @ManyToMany
    @JoinTable(name = "book_author",
        joinColumns = { @JoinColumn(name = "fk_book") },
        inverseJoinColumns = { @JoinColumn(name = "fk_author") })
    private List<Author> authors = new ArrayList<Author>();
 
    ...
}

2 个实体:

@Entity
public class Author {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
 
    @ManyToMany(mappedBy="authors")
    private List<Book> books = new ArrayList<Book>();
 
    ...
}

这就是定义双向多对多关联所需要做的全部工作。现在,您可以在 JPQL 或 Criteria API 查询中或在域对象上双向导航。

b = em.find(Book.class, 1L);
List<Author> authors = b.getAuthors();

双向关联在查询中很容易使用,但在持久保存新实体时还需要执行额外的步骤。
当您添加或删除实体时,您需要更新双方的关联。
您可以在以下代码片段中看到它的示例,其中我首先创建一个新的作者实体并将图书实体添加到图书列表中。
之后,我还需要将新的作者实体添加到图书实体的作者列表中。

Book b = em.find(Book.class, 1L);
 
Author a = new Author();
a.setFirstName("Thorben");
a.setLastName("Janssen");
 
a.getBooks().add(b);
b.getAuthors().add(a);
 
em.persist(a);

更新两个实体上的关联是一项容易出错的任务。因此,为其提供辅助方法是一个很好的做法。

@Entity
public class Author {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
 
    @ManyToMany(mappedBy="authors")
    private List<Book> books = new ArrayList<Book>();
 
    public void addBook(Book b) {
        this.books.add(b);
        b.getAuthors().add(this);
    }
 
    public void removeBook(Book b) {
        this.books.remove(b);
        b.getAuthors().remove(this);
    }
 
    ...
}

请参阅:https://thorben-janssen.com/hibernate -tips-map-双向-多多关联/

1 Entity:

@Entity
public class Book {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
 
    @ManyToMany
    @JoinTable(name = "book_author",
        joinColumns = { @JoinColumn(name = "fk_book") },
        inverseJoinColumns = { @JoinColumn(name = "fk_author") })
    private List<Author> authors = new ArrayList<Author>();
 
    ...
}

2 Entity:

@Entity
public class Author {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
 
    @ManyToMany(mappedBy="authors")
    private List<Book> books = new ArrayList<Book>();
 
    ...
}

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.

b = em.find(Book.class, 1L);
List<Author> authors = b.getAuthors();

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.

Book b = em.find(Book.class, 1L);
 
Author a = new Author();
a.setFirstName("Thorben");
a.setLastName("Janssen");
 
a.getBooks().add(b);
b.getAuthors().add(a);
 
em.persist(a);

Updating the associations on both entities is an error-prone task. It’s, therefore, a good practice to provide helper methods for it.

@Entity
public class Author {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
 
    @ManyToMany(mappedBy="authors")
    private List<Book> books = new ArrayList<Book>();
 
    public void addBook(Book b) {
        this.books.add(b);
        b.getAuthors().add(this);
    }
 
    public void removeBook(Book b) {
        this.books.remove(b);
        b.getAuthors().remove(this);
    }
 
    ...
}

see: https://thorben-janssen.com/hibernate-tips-map-bidirectional-many-many-association/

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