如何使用@ElementCollection进行Set 映射?

问题描述 投票:0回答:1

我有2张桌子。 report_id是PK。 animals_in_area没有PK键。

我需要根据给定的animal_idanimal_report检查report_id中的report_date。这似乎是一个简单的任务,但是我可以以某种方式描述Set<Integer> adimalIds类中的字段animal_report,该字段映射到请求的区域吗?我尝试了@ElementCollection + @CollectionTable,但是由于report_id批注,它会自动将area_id映射到@Id

@ElementCollection
@CollectionTable(name = "animals_in_area", joinColumns = @JoinColumn(name = "area_id"))
@Column(name = "animal_id")
private Set<Integer> adimalIds = new HashSet<>();

我想在DTO中使用criteriaBuilder.isMember(animalIdRequest, adimalIds)。>>

经典方法是什么?加入并获得Set吗?

               animal_report                                 animals_in_area
    ---------------------------------                      -------------------
    report_id | report_date | area_id                      area_id | animal_id  
    ---------------------------------                      -------------------
        1     |  01.01.2020 |  100                           100   |   1001
        2     |  01.01.2020 |  101                           100   |   1002
                                                             100   |   1003
                   .....                                     101   |   1002
                                                             101   |   1004

@Entity
@Table(name = "animal_report")
public class AnimalReport implements Serializable {

  @Id
  @Column(name = "report_id")
  private Long reportId;

  @Column(name = "report_date")
  private LocalDate reportDate;

  @Column(name = "area_id")
  private Integer areaId;
  ...
}

我有2张桌子。 report_id是PK。 animals_in_area没有PK密钥。我需要通过给定的report_id和report_date来检查animal_report中的animal_id。这似乎很简单,但是可以...

java hibernate join one-to-many hibernate-onetomany
1个回答
0
投票

您应通过以下方式更正映射:

© www.soinside.com 2019 - 2024. All rights reserved.