【问题标题】:Spring Boot auto generate association table with attibuteSpring Boot 自动生成带属性的关联表
【发布时间】:2021-11-01 15:42:18
【问题描述】:

我在数据库中自动生成实体,使用效果很好:

spring.jpa.hibernate.ddl-auto=update

现在我需要关联表中的一个附加属性,如果不手动创建关联实体,我无法弄清楚。下面列出的当前类生成一个没有附加属性的关联表。

我需要的桌子:

    persons:
    id
    
    search_requests:
    id
    
    search_requests_persons:
    person_id
    search_request_id
    study_id  

我目前拥有的课程(简化):

@NoArgsConstructor
@RequiredArgsConstructor(staticName = "of")
@Getter
@Setter
@Entity
@EqualsAndHashCode
@Table(name = "persons")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private Long id;
    
    // The following line is what I would need
    // private List<Integer> studyIds;
}
@NoArgsConstructor
@RequiredArgsConstructor(staticName = "of")
@Getter
@Setter
@Entity
@EqualsAndHashCode
@Table(name = "search_requests")
public class SearchRequest {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private Long id;

    @NonNull
    @ManyToMany
    private List<Person> persons;
}

我将 Lombok 和 Javax Persistence 用于注释。

【问题讨论】:

    标签: java spring spring-boot jpa


    【解决方案1】:

    为了在您的JoinTable 中添加其他属性,您可以手动创建您的JoinTable 实体。例如: PersonSearchRequest.java

    @Entity
    @Table(name = "person_search_request)
    @Getter
    @Setter
    @NoArgsConstructor
    public class PersonSearchRequest {
        @EmbeddedId
        private PersonSearchRequestPK id;
        
        // put your additional attribute here, e.g:
        private String attribute1;
        private Long attribute2;
        @ElementCollection
        private List<String> attribute3s; 
    
        @Embeddable
        @Getter
        @Setter
        @AllArgsConstructor
        @NoArgsConstructor
        public static class PersonSearchRequestPK implements Serializable {
            @ManyToOne(fetch = FetchType.LAZY)
            private Person person;
    
            @ManyToOne(fetch = FetchType.LAZY)
            private SearchRequest searchRequest;
        }
    
    }
    

    Person.java

    @NoArgsConstructor
    @RequiredArgsConstructor(staticName = "of")
    @Getter
    @Setter
    @Entity
    @EqualsAndHashCode
    @Table(name = "persons")
    public class Person {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @JsonProperty(access = JsonProperty.Access.READ_ONLY)
        private Long id;
        
        @OneToMany(mappedBy = "id.person")
        private List<PersonSearchRequest> personSearchRequests;
    }
    

    SearchRequest.java

    @NoArgsConstructor
    @RequiredArgsConstructor(staticName = "of")
    @Getter
    @Setter
    @Entity
    @EqualsAndHashCode
    @Table(name = "search_requests")
    public class SearchRequest {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @JsonProperty(access = JsonProperty.Access.READ_ONLY)
        private Long id;
    
        @OneToMany(mappedBy = "id.searchRequest")
        private List<PersonSearchRequest> personSearchRequests;
    }
    

    【讨论】:

    • 使用这个我不能在我的 SearchRequest.class 中映射private List&lt;Person&gt; persons;
    • 是的,您无法直接从SearchRequest 获取人员列表,但您可以通过以下方式获取:searchRequest.getPersonSearchRequests().stream().map(x -&gt; x.getId().getPerson()).collect(Collectors.toList()))
    猜你喜欢
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2014-02-02
    • 2022-01-16
    • 2017-07-30
    • 2019-04-21
    • 2019-02-18
    相关资源
    最近更新 更多