JPARepository没有保存@JoinColumn

时间:2018-04-20 18:56:25

标签: java hibernate spring-boot jpa spring-data-jpa

我有以下实体关系:

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
@Entity
public class BatchProcessRecord implements Serializable, Cloneable {

    ...

    @ManyToOne
    @JoinColumn(name = "batch_process_id")
    private BatchProcess batchProcess;
}


@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
@Entity
public class BatchProcess implements Serializable, Cloneable {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "batchProcess")
    private List<BatchProcessRecord> records;
}

以下代码保存实体:

    // Create batch process
    BatchProcess batchProcess = BatchProcess.builder()
            ....
            .records(records)
            .build();

    // Save entity
    batchProcessRepository.save(batchProcess);

在控制台中,它按预期生成插入(带有2个子节点),但不填充batch_process_id(JoinColumn)。

发生了什么事?

控制台:

Hibernate: insert into batch_process (batch_end_date, batch_entries_count, batch_entries_with_issues, batch_name, batch_start_date, report_file_download_date, report_file_name, result_file_name, result_file_upload_date, status) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: insert into batch_process_record (address_number, batch_process_id, city, first_name, full_street_address, last_name, maternal_name, post_directional, pre_directional, reference_number, ssn, state, street_type, zip_code) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: insert into batch_process_record (address_number, batch_process_id, city, first_name, full_street_address, last_name, maternal_name, post_directional, pre_directional, reference_number, ssn, state, street_type, zip_code) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2

存储库:

public interface BatchProcessRepository extends 
JpaRepository<BatchProcess, Long> {
}

0 个答案:

没有答案