【问题标题】:JPA and insert into partition of tableJPA并插入表的分区
【发布时间】:2015-10-20 14:54:30
【问题描述】:

我在 PostgreSQL 中有一个表,其中有 n 个分区(TABLE_1、TABLE_2、...、TABLE_n)。有一个触发器,当您插入表 TABLE 时,会将数据插入分区。 触发器:

IF (NEW.program_id=1) THEN INSERT INTO st.table_1 VALUES (NEW.*);
        ELSIF (NEW.program_id=2) THEN INSERT INTO st.table_2 VALUES (NEW.*);
 ....
return NEW;

我有一个对象:

@Entity
@Table(name = "TABLE", schema = "st")
public class TABLE implements Serializable {


@Getter
@Setter
@Column(name = "id", nullable = false, insertable = false, columnDefinition = "integer auto_increment")
private Integer id;


@Getter
@Setter
@Column(name = "program_id", nullable = false)
private Integer programId;

.... }

我有一个 JpaRepository:

public interface TableRepository extends JpaRepository<TABLE, Integer> {}

所以,当我保存对象 TABLE 时:

TableRepository.save(myTable);

记录重复...多个表TABLE中的一条记录,分区中的第二条记录...

如何制作只存在于分区中的记录??

UPD。

我的@SQLInsert 示例

@SQLInsert(sql = "INSERT INTO store.invoice(id,program_id,db_id,acs_number,is_active,date_doc,summa,is_entry," +
             "invoice_type_id,from_id,to_id,from_req_id,to_req_id,agent_id,offic,bn,date_create,date_update," +
             "offic_number,comment,date_ship,is_edited,store_rule_id,inv_cc_days,fact_date_doc,update_id,store_doc," +
             "order_num,parent_num,addition_num,user_create,user_update,place_id,type_data_col_id,contract_id," +
             "delivery,cert_num,uid,original_uid,date_return,reason_get_back_id,entry_night,send_stm,time_send_stm," +
             "invoice_type_for_1c,isa_id,isa_status_id,uid_ishop_id,delivery_ag_id,fraction_offic_number," +
             "forwarder_id,declarant_id,ext_locked,declarant_employee_num,revision_id,src_doc_sum,op_num," +
             "is_factoring,is_add_2amounts,is_load_2isa,num_sf,id_from_1c)" +
             "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", check = ResultCheckStyle.NONE)

【问题讨论】:

    标签: java hibernate postgresql jpa partitioning


    【解决方案1】:

    错误不在 JPA 中,而是在您的触发器中:return NEW; 将始终执行原始的 INSERT,您只想在触发器中执行您的 INSERT

    您应该改用return null;,以取消原来的INSERT INTO TABLE

    【讨论】:

    • 当我使用 'return null' 时,出现错误'批量更新从更新 [0] 返回了意外的行数;实际行数:0;预期:1 ' 插入新记录时...
    • 您在触发器中使用SET NOCOUNT ON; 吗?在这种情况下,您将不得不放弃它,因为它会在触发器中忽略您的 INSERT 以获取总行数。
    • 我忘记了 Postgres 不支持更改受影响的行数。在这种情况下,您可以将 @SQLInsert 用于您的实体类 - 这通常会忽略结果计数。
    • 但我有错误:'Caused by: org.postgresql.util.PSQLException: No value for parametr of 62.'
    • 您应该检查生成的 SQL - 如果没有更多信息,很难猜测此错误的来源。
    猜你喜欢
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2012-07-21
    相关资源
    最近更新 更多