如果使用長爲ID(而不是UUID),那麼很可能你的ID會從1開始並匹配相同的順序記錄出現在你data.sql實體間關係的
例子:
@Entity
public class Document {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false, length=256)
@Size(min=1,max=256)
private String name;
private String code;
private Date date;
@OneToMany(mappedBy = "document", cascade = CascadeType.ALL)
@JsonManagedReference
private List<DocumentItem> documentItems;
@Entity(name="document_item")
public class DocumentItem {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false, length=256)
private String name;
private BigDecimal price;
@ManyToOne(optional = false)
@JsonBackReference
private Document document;
在示例列DOCUMENT_ID上述將被添加到document_item實體。
你也可以指定與JoinColumn註解列名其中,用於關係,例如:
@ManyToOne(optional = false)
@JoinColumn(name = "publisher_id")
private Publisher publisher;
實例初始化腳本:
INSERT INTO document(name, code) VALUES ('Test Document 1', 'TD-1');
INSERT INTO document(name, code) VALUES ('Test Document 2', 'TD-2');
INSERT INTO document(name, code) VALUES ('Test Document 3', 'TD-3');
INSERT INTO document(name, code) VALUES ('Test Document 4', 'TD-4');
INSERT INTO document(name, code) VALUES ('Test Document 5', 'TD-5');
INSERT INTO document_item(document_id, name, price) VALUES (2,'Test Item 2.A', 100.45);
INSERT INTO document_item(document_id, name, price) VALUES (2,'Test Item 2.B', 30.45);
INSERT INTO document_item(document_id, name, price) VALUES (2,'Test Item 2.C', 564);
INSERT INTO document_item(document_id, name, price) VALUES (4,'Test Item 4.A', 1);
INSERT INTO document_item(document_id, name, price) VALUES (4,'Test Item 4.B', 456);
INSERT INTO document_item(document_id, name, price) VALUES (5,'Test Item 5.A', 67);
INSERT INTO document_item(document_id, name, price) VALUES (2,'Test Item 2.D', 98);
你寫「更可能你的ID會從開始1「......這不是」只是一個可能的合理性「(同時我發現)可以_配置ID如何生成DB。 @GeneratedValue((策略= GenerationType.AUTO)或@GeneratedValue(策略= GenerationType.SEQUENCE) @SequenceGenerator(NAME = 「序列」,allocationSize = 10),後者會給你連續編號。 – Robert
@Robert,你是對的。我只是沒有深入瞭解身份證生成策略的細節,但留下了一些空間,說「可能..」 – Alexander