【问题标题】:Web application spring boot and angular 5Web 应用程序 Spring Boot 和 Angular 5
【发布时间】:2018-09-27 02:59:12
【问题描述】:

我正在使用 Spring boot 和 MongoDB 制作一个简单的 CRUD 应用程序,我面临的问题是我不知道如何定义模型类。

我的应用程序应该是这样的:

网站具有一些特征,例如 ID、地区、城市……并包含 4 个部分(蜂窝),每个部分都有自己的特征。任何帮助,将不胜感激。

这是我目前所拥有的:

public class Site {

    @Id
    String siteId;
    String projectPhase;
    String region;
    String city;
    String siteName;
    String newSiteName;
    String clusterName ;
    String longitude ;
    String lattitude ;

    @OneToMany(mappedBy = "siteId")
    List  L;

我想知道的是如何在这个类中关联另一个类。

【问题讨论】:

  • 问题似乎是关于 ORM/JPA(由 Spring Boot 使用),而不是关于 typescript、mongo 或 angular5。此外,目前还不清楚您尝试了什么,以及您想关联什么其他类。提供有关您的确切问题的更多详细信息;就问题而言,无法回答。

标签: java spring spring-boot spring-data-mongodb


【解决方案1】:

@OneToMany 之类的注解通常在 JPA 上下文中使用,并且在使用 Spring Data MongoDB 时是不必要的。 the documentation也提到了这一点:

没有必要使用@OneToMany 之类的东西,因为映射框架认为您需要一对多关系,因为存在List 对象。

当您想在使用 MongoDB 时定义一对多关系时,您有几个选择。其中第一个是将它们定义为同一文档中的嵌入对象:

@Document
public class Site {
     @Id
     private String id;
     private String city;
     private String region;
     private List<Part> cellulars;
}

public class Part {
    private String characteristic1;
    private String characteristic2;
}

这意味着这些部分并不独立存在,因此它们也不需要自己的 ID。

另一种可能是参考另一个文件:

@Document
public class Site {
     @Id
     private String id;
     private String city;
     private String region;
     @DBRef
     private List<Part> cellulars;
}

@Document
public class Part {
    @Id
    private String id;
    private String characteristic1;
    private String characteristic2;
}

在这种情况下,部件也是单独的文档,并且站点只包含对该部件的引用。

【讨论】:

  • 非常感谢您的回答,这很有帮助。
猜你喜欢
  • 2018-10-31
  • 2019-08-31
  • 2017-10-07
  • 2019-08-17
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
相关资源
最近更新 更多