使用单个JPA查询在多个表中插入数据

时间:2018-11-16 10:44:40

标签: java jpa spring-data-jpa spring-data jpa-2.0

我想使用多个表为风险过滤器创建JPA查询。我有这些表要用于存储数据:

风险过滤器:

@Entity
@Table(name = "risk_filters")
public class RiskFilters implements Serializable {

    private static final long serialVersionUID = -31232741713890134L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, updatable = false, nullable = false)
    private int id;

    @Column(length = 4)
    private Integer terminal_id;

    @Column(length = 255)
    private String filter_class;

    @Column(name = "FILTER_SETTINGS", columnDefinition = "TEXT", length = 65535)
    private String filter_settings;

    @Column(length = 4)
    private Integer position;

    @Column(length = 4)
    private Integer parent_id;

    @Column
    private LocalDateTime created_at;

    @Column
    private TemporalType updated_at;
    .....
}

风险参数:

@Entity
@Table(name = "risk_parameters")
public class RiskParameters implements Serializable {

    private static final long serialVersionUID = 6828168899788931021L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, updatable = false, nullable = false)
    private int id;

    @Column(length = 4)
    private Integer payment_transaction_id;

    @Column(length = 32)
    private String name;

    @Column(length = 128)
    private String value;

    @Column
    private LocalDateTime created_at;

    @Column
    private LocalDateTime updated_at;
    ....
}

风险详情:

@Entity
@Table(name = "risk_details")
public class RiskDetails implements Serializable {

    private static final long serialVersionUID = 6122869200132217334L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, updatable = false, nullable = false)
    private int id;

    @Column(length = 4)
    private Integer payment_transaction_id;

    @Column(length = 32)
    private String service_name;

    @Column(length = 64)
    private String name;

    @Column(length = 512)
    private String value;
    .....
}

如何通过一次插入将数据插入这些实体并通过一次选择获得数据?

我想使用JPA来获取和插入数据。

1 个答案:

答案 0 :(得分:0)

如果您在表(risk_filtersrisk_parametersrisk_details之间有关系,例如主键外键,则可能关系。

假设 risk_filters risk_parameters risk_details 具有一对一关系,则实体将看起来像

@Entity
@Table(name = "risk_filters")
public class RiskFilters implements Serializable {

private static final long serialVersionUID = -31232741713890134L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, updatable = false, nullable = false)
private int id;

@Column(length = 4)
private Integer terminal_id;

@Column(length = 255)
private String filter_class;

@Column(name = "FILTER_SETTINGS", columnDefinition = "TEXT", length = 65535)
private String filter_settings;

@Column(length = 4)
private Integer position;

@Column(length = 4)
private Integer parent_id;

@Column
private LocalDateTime created_at;

@Column
private TemporalType updated_at;

@OneToOne
@JoinColumn(name="risk_filter_id", nullable=false)
private RiskParameters riskParameters;

@OneToOne
@JoinColumn(name="risk_filter_id", nullable=false)
private RiskDetails riskDetails;
.....
}

和调用保存方法,仅保存 RiskFilters 实体,如下所示。

riskFilterObject.setRiskDetails(riskDetailsObject);
riskFilterObject.setRiskParamarets(riskParametersObject);
repository.save(riskFilterObject);// update 3 tables
repository.save(riskDetailsObject); // update only risk_details table
repository.save(riskParametersObject);// update only risk_parameters table

更新

  

如何选择?你能告诉我如何实施全部选择吗   实体?

RiskFilters filters = riskFiltersRepository.findOne(id);
//用于完整的网状 RiskFilters 实体对象

RiskParameter param = riskParameterRepository.findOne(id);
//仅用于 RiskParameter 实体对象

RiskDetails details = riskDetailsRepository.findOne(id);
//仅用于 RiskDetails 实体对象