PostgreSQL
 sql >> база данни >  >> RDS >> PostgreSQL

Как да моделираме пакети, версии и лицензи?

Бяхте доста близо до намирането на решението.

Следната настройка работи:

NpmPackage

@Entity
@Table(name = "npm_package")
public class NpmPackage {

  public NpmPackage(String name, String description) {
    this.name = name;
    this.description = description;
  }

  public NpmPackage() {
  }

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;

  private String description;

  @OneToMany(mappedBy = "npmPackage", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
  private List<NpmPackageVersion> versions = new ArrayList<>();

  ...//getters and setters

  public void addVersion(NpmPackageVersion version) {
    this.versions.add(version);
    version.setNpmPackage(this);
  }

  public void removeVersion(NpmPackageVersion version) {
    this.versions.remove(version);
  }
}

NpmPackageVersion

@Entity
@Table(name = "npm_package_version")
public class NpmPackageVersion {

    public NpmPackageVersion(String version, License license) {
        setVersion(version);
        this.license = license;
    }

    public NpmPackageVersion() {
    }

    @EmbeddedId
    private NpmPackageIdVersion npmPackageIdVersion = new NpmPackageIdVersion();

    @MapsId("npmPackageId")
    @ManyToOne
    private NpmPackage npmPackage;

    @ManyToOne
    private License license;

    public String getVersion() {
        return npmPackageIdVersion.version;
    }

    public void setVersion(String version) {
        npmPackageIdVersion.version = version;
    }

    ...//getters and setters

    @Embeddable
    public static class NpmPackageIdVersion implements Serializable {
        private static final long serialVersionUID = 3357194191099820556L;

        private Long npmPackageId;

        private String version;

        ... //getters and setters etc.
    }
}

Лиценз

@Entity
@Table(name = "license")
public class License {

    public License() {}

    public License(String name, String licenseId) {
        this.name = name;
        this.licenseId = licenseId;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String reference;

    @Column(name = "is_deprecated_license_id")
    private boolean deprecatedLicenseId;

    private Integer referenceNumber;

    private String name;

    private String licenseId;

    @Column(name = "is_osi_approved")
    private boolean osiApproved;

    ... //getters and setters
}

Използвах следния код, за да го тествам:

@Transactional
public NpmPackage createPackage() {
        License license = new License("General Public License", "GPL 2.0");
        em.persist(license);
        NpmPackage npmPackage = new NpmPackage("react", "React Framework");
        npmPackage.addVersion(new NpmPackageVersion("8.11.0", license));
        em.persist(npmPackage);
}



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. NodeJS/Knex Създаване на Json отговор

  2. Урок за Postgres:pg_restore:входният файл [archiver] не изглежда като валиден архив

  3. Добавяне на външен ключ към релсов модел

  4. Как да търсите конкретна стойност във всички таблици (PostgreSQL)?

  5. Преобразувайте обектен масив в масив, съвместим за nodejs/pg/unnest