Hibernate join get Set 没有类的字段

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

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

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


public class AnimalReport {
    @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;
        ...
    }
java hibernate join one-to-many hibernate-onetomany
1个回答
0
投票

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

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

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

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