我有一个表WebsitePages,其中包含有关页面的信息,但是我需要列sequence可以被数据库重复并增加。

CREATE TABLE `WebsitePages` (
  `id`                   INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  `sequence`             INT UNSIGNED              NOT NULL,
  `title`                VARCHAR(255)              NOT NULL,
  `url`                  VARCHAR(255)              NOT NULL,
  `revision_id`          INT UNSIGNED DEFAULT 1    NOT NULL,

  UNIQUE `unique_pair` (`sequence`, `revision_id`),
  FOREIGN KEY (`sequence`) REFERENCES `WebsitePageSequence` (`id`),
);


WebsitePages的顺序表:

CREATE TABLE `WebsitePageSequence` (
  `id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT
);


我还有一个表,该表包含此页面上有关小部件的信息,并且它还具有sequence列,并且应通过WebsitePages列将其与page_seq链接:

CREATE TABLE `WidgetSequence` (
  `id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT
);

CREATE TABLE `Widgets` (
  `id`         INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  `sequence`   INT UNSIGNED           NOT NULL,
  `page_seq`   INT UNSIGNED           NOT NULL,
  `data`       LONGTEXT,
  `revision`   INT UNSIGNED DEFAULT 1 NOT NULL,

  UNIQUE `unique_pair` (`sequence`, `revision`),
  INDEX `idx_performance` (`page_seq`, `revision`),
  FOREIGN KEY (`sequence`) REFERENCES `WidgetSequence` (`id`),
  FOREIGN KEY (`page_seq`) REFERENCES `WebsitePages` (`sequence`),
);


我有一个像这样的Java实体:

// Sequence.java
@MappedSuperclass
abstract public class Sequence implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "`id`")
    protected Long id;

get; set;
}

// WebsitePageSequence.java
@Entity
@Table(name = "`WebsitePageSequence`")
public class WebsitePageSequence extends Sequence {
}

// WidgetSequence.java
@Entity
@Table(name = "`WidgetSequence`")
public class WidgetSequence extends Sequence {
}

// Widget.java
@Entity
@Table(name = "`Widgets`")
public class Widget {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "`id`")
    protected Long id;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "sequence")
    protected WidgetSequence sequence = new WidgetSequence();

    @Column(name = "`data`", nullable = true)
    protected String data;

    // PROBLEM IN THIS CODE
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "`page_seq`", referencedColumnName = "`sequence`")
    protected WebsitePageData pageData;
    // --------------------

    @Column(name = "`revision`")
    protected Long revision;

get; set;
}

@Entity
@Table(name = "`WebsitePage`")
public class WebsitePageData {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "`id`")
    protected Long id;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "`sequence`")
    protected WebsitePageSequence sequence = new WebsitePageSequence();

    @Column(name = "`title`", nullable = false)
    protected String title;

    @Column(name = "`url`", nullable = false)
    protected String url;

    @Column(name = "`revision_id`", nullable = false)
    protected Integer revisionId;

    // HERE I WANT TO FETCH ALL WIDGETS, THAT LINKED BY `Sequences`
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "pageData")
    protected List<WidgetData> widgets;

get; set;
}


但是,当我尝试使用小部件获取WebsitePage时,我从休眠中得到了错误:

...Widget column: page_seq (should be mapped with insert="false" update="false")

最佳答案

它固定与

// PROBLEM IN THIS CODE
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "`page_seq`", referencedColumnName = "`sequence`", insertable = false, updatable = false)
protected WebsitePageData pageData;
// --------------------

08-24 17:41