Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA and insert into partition of table

I have a TABLE in PostgreSQL, in which n partitions (TABLE_1, TABLE_2, ..., TABLE_n). There is a trigger that when you insert a table TABLE, inserts data into a partition. Trigger:

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;

I have an object:

@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;

.... }

I have a JpaRepository:

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

So, when I save the object TABLE:

TableRepository.save(myTable);

record duplicated... One record in several table TABLE, the second record in partition...

How to make a record that was only in the partition ??

UPD.

Example my @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)
like image 500
John Avatar asked Nov 10 '22 08:11

John


1 Answers

The error is not in JPA, but in your trigger: return NEW; will always execute the original INSERT, where you only want to execute your INSERT in the trigger.

You should use return null; instead, to cancel the original INSERT INTO TABLE.

like image 124
Tobias Liefke Avatar answered Nov 14 '22 23:11

Tobias Liefke