Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有附加字段的JPA多个映射_Java_Hibernate_Postgresql_Jpa - Fatal编程技术网

Java 带有附加字段的JPA多个映射

Java 带有附加字段的JPA多个映射,java,hibernate,postgresql,jpa,Java,Hibernate,Postgresql,Jpa,我有下面三个表(PostgreSQL): 以及以下JPA实体: @Entity @Table(name = "boards") public class Board extends BaseEntity implements Serializable { @Id @SequenceGenerator(name = "boards_id_seq", sequenceName = "boards_id_seq", allocationSize = 1) @Generated

我有下面三个表(PostgreSQL):

以及以下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;

如何正确组织映射以获得
板卡。对于通过
多个
关系映射到
板卡
表的某个
,这只是一个带有额外列的关系表,这个问题经常出现,所以你应该能够通过搜索找到许多其他答案、示例和教程

不过,我相信最好的解决方案是将关系表映射为一个实体,让您能够完全控制字段和数据。然后,您可以随时将其映射到您的模型中,甚至在需要时对模型隐藏其存在。例如:

@Entity
@Table(name = "boards_cards")
public class Board_Card extends BaseEntity implements Serializable {
  @Id
  @ManyToOne
  Board board;
  @Id
  @ManyToOne
  Card card;
  Boolean on_hold;
}
然后,无论何时需要,都可以在您的卡和板实体中引用。一个选项可能是隐藏卡板实体,并且仅返回卡板实体:

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;
  }
公共类板扩展BaseEntity实现可序列化{
..
@OneToMany(mappedby=“董事会”)
列表卡、板、列表;
@短暂的
名单卡;
公开名单{
如果(卡片==null){
cards=新的ArrayList();
用于(卡板cb:卡板列表){
add(cb.getCard());
}
}
回程卡;
}

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

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

不过,我认为最好的解决方案是将关系表映射为一个实体,让您完全控制字段和数据。然后,您可以根据需要将其映射到模型中,甚至在需要时对模型隐藏其存在性。例如:

@Entity
@Table(name = "boards_cards")
public class Board_Card extends BaseEntity implements Serializable {
  @Id
  @ManyToOne
  Board board;
  @Id
  @ManyToOne
  Card card;
  Boolean on_hold;
}
然后,可以根据需要在您的卡和板实体中引用此项。一个选项可能是隐藏卡和板实体,并且仅返回卡实体:

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;
  }
公共类板扩展BaseEntity实现可序列化{
..
@OneToMany(mappedby=“董事会”)
列表卡、板、列表;
@短暂的
名单卡;
公开名单{
如果(卡片==null){
cards=新的ArrayList();
用于(卡板cb:卡板列表){
add(cb.getCard());
}
}
回程卡;
}

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

chceck这个列表稍微相关,但使用IDENTITY sequence generator可以在pg中工作并使事情变得复杂easier@Niel-McGuigan身份不允许预分配,并且有其他限制-它如何使事情变得更容易?chceck这稍微有点关系ted,但使用身份序列生成器在pg中工作,并使事情easier@Niel-McGuigan标识不允许预分配,并且有其他限制-它如何使事情变得更容易?