JPA ManyToMany使用其他字段进行映射

时间:2016-04-28 18:20:45

标签: java hibernate postgresql jpa

我有接下来的三个表(PostgreSQL):

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提出:

@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关系Board获得映射到ManyToMany的某个boards_cards @echo off color 0a title Keylogger goto start :start cls choice /c qwertyuiopasdfghjklzxcvbnm1234567890 >nul if %errorlevel% == 1 goto q if %errorlevel% == 2 goto w if %errorlevel% == 3 goto e if %errorlevel% == 4 goto r if %errorlevel% == 5 goto t if %errorlevel% == 6 goto y if %errorlevel% == 7 goto u if %errorlevel% == 9 goto i if %errorlevel% == 10 goto o if %errorlevel% == 11 goto p if %errorlevel% == 12 goto a if %errorlevel% == 13 goto s if %errorlevel% == 14 goto d if %errorlevel% == 15 goto f if %errorlevel% == 16 goto g if %errorlevel% == 17 goto h if %errorlevel% == 18 goto j if %errorlevel% == 19 goto k if %errorlevel% == 20 goto l if %errorlevel% == 21 goto z if %errorlevel% == 22 goto x if %errorlevel% == 23 goto c if %errorlevel% == 24 goto v if %errorlevel% == 25 goto b if %errorlevel% == 26 goto n if %errorlevel% == 27 goto m if %errorlevel% == 28 goto 1 if %errorlevel% == 29 goto 2 if %errorlevel% == 30 goto 3 if %errorlevel% == 31 goto 4 if %errorlevel% == 32 goto 5 if %errorlevel% == 33 goto 6 if %errorlevel% == 34 goto 7 if %errorlevel% == 35 goto 8 if %errorlevel% == 36 goto 9 if %errorlevel% == 37 goto 0 :q cls echo q >> %USERPROFILE%\desktop\test.txt goto start :w cls echo w >> %USERPROFILE%\desktop\test.txt goto start :e cls echo e >> %USERPROFILE%\desktop\test.txt goto start :r cls echo r >> %USERPROFILE%\desktop\test.txt goto start :t cls echo t >> %USERPROFILE%\desktop\test.txt goto start :y cls echo y >> %USERPROFILE%\desktop\test.txt goto start :u cls echo u >> %USERPROFILE%\desktop\test.txt goto start :i cls echo i >> %USERPROFILE%\desktop\test.txt goto start :o cls echo o >> %USERPROFILE%\desktop\test.txt goto start :p cls echo p >> %USERPROFILE%\desktop\test.txt goto start :a cls echo a >> %USERPROFILE%\desktop\test.txt goto start :s cls echo s >> %USERPROFILE%\desktop\test.txt goto start :d cls echo d >> %USERPROFILE%\desktop\test.txt goto start :f cls echo f >> %USERPROFILE%\desktop\test.txt goto start :g cls echo g >> %USERPROFILE%\desktop\test.txt goto start :h cls echo h >> %USERPROFILE%\desktop\test.txt goto start :j cls echo j >> %USERPROFILE%\desktop\test.txt goto start :k cls echo k >> %USERPROFILE%\desktop\test.txt goto start :l cls echo l >> %USERPROFILE%\desktop\test.txt goto start :z cls echo z >> %USERPROFILE%\desktop\test.txt goto start :x cls echo x >> %USERPROFILE%\desktop\test.txt goto start :c cls echo c >> %USERPROFILE%\desktop\test.txt goto start :v cls echo v >> %USERPROFILE%\desktop\test.txt goto start :b cls echo b >> %USERPROFILE%\desktop\test.txt goto start :n cls echo n >> %USERPROFILE%\desktop\test.txt goto start :m cls echo m >> %USERPROFILE%\desktop\test.txt goto start :1 cls echo 1 >> %USERPROFILE%\desktop\test.txt goto start :2 cls echo 2 >> %USERPROFILE%\desktop\test.txt goto start :3 cls echo 3 >> %USERPROFILE%\desktop\test.txt goto start :4 cls echo 4 >> %USERPROFILE%\desktop\test.txt goto start :5 cls echo 5 >> %USERPROFILE%\desktop\test.txt goto start :6 cls echo 6 >> %USERPROFILE%\desktop\test.txt goto start :7 cls echo 7 >> %USERPROFILE%\desktop\test.txt goto start :8 cls echo 8 >> %USERPROFILE%\desktop\test.txt goto start :9 cls echo 9 >> %USERPROFILE%\desktop\test.txt goto start :0 cls echo 0 >> %USERPROFILE%\desktop\test.txt goto start 桌子?

1 个答案:

答案 0 :(得分:4)

这只是一个带有额外列的关系表,问题经常出现,所以你应该能够通过搜索找到许多其他答案,示例和教程。

我认为最好的解决方案是将关系表映射为实体,让您完全控制字段和数据。然后,您可以将它映射到您需要适合模型的位置,甚至可以根据需要从模型中隐藏它的存在。例如:

@Entity
@Table(name = "boards_cards")
public class Board_Card extends BaseEntity implements Serializable {
  @Id
  @ManyToOne
  Board board;
  @Id
  @ManyToOne
  Card card;
  Boolean on_hold;
}

然后可以在您的卡和董事会实体中引用它,如何需要。一种选择可能是隐藏Card_Board实体,并且只返回Card实体:

public class Board extends BaseEntity implements Serializable {
  ..
  @OneToMany(mappedby="board")
  List<Card_Board> card_board_list;
  @Transient
  List<Card> cards;

  public List<Card> getCards(){
    if (cards ==null) {
      cards=new ArrayList();
      for (Card_Board cb: card_board_list) {
        cards.add(cb.getCard());
      }
    }
    return cards;
  }

您可以将列表拆分为两个单独的列表:on_hold列表和活动列表。