1
我有一個未來三個表(PostgreSQL的):JPA多對多映射與附加字段
CREATE TABLE boards (
id SERIAL PRIMARY KEY,
....
);
CREATE TABLE IF NOT EXISTS cards (
id SERIAL PRIMARY KEY,
name VARCHAR(256),
description TEXT,
...
);
CREATE TABLE IF NOT EXISTS boards_cards(
board_id INTEGER,
card_id INTEGER,
on_hold BOOLEAN DEFAULT FALSE,
CONSTRAINT pk_user_card PRIMARY KEY (board_id, card_id),
FOREIGN KEY(board_id) REFERENCES boards(id),
FOREIGN KEY(card_id) REFERENCES cards(id)
);
及以下JPA的entites:
@Entity
@Table(name = "boards")
public class Board extends BaseEntity implements Serializable {
@Id
@SequenceGenerator(name = "boards_id_seq", sequenceName = "boards_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "boards_id_seq")
private Long id;
@Entity
@Table(name = "cards")
public class Card extends BaseEntity implements Serializable {
@Id
@SequenceGenerator(name = "cards_id_seq", sequenceName = "cards_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "cards_id_seq")
private Long id;
private String name;
private String description;
如何正確組織映射,以便能夠得到boards_cards.on_hold
爲Card
,通過ManyToMany
映射到Board
關係在boards_cards
表上?
chceck this out http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/ – mariusz2108
勉強相關,但使用IDENTITY序列生成器工作在PG和使事情更容易 –
@ Niel-McGuigan身份不允許預先分配和其他退後 - 它是如何使事情變得更容易? https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Identity_sequencing – Chris