Spring Data JPA与Extras列orphan进行多对多移除不起作用

时间:2019-02-05 13:51:43

标签: java hibernate jpa spring-data-jpa many-to-many

我有两个有着多对多关系的实体。

CommercialePiece -)-------------(- Transaction

他们需要额外的信息,所以我创建了一个可联合的名为piecetransaction

因此,这种关系变成了---- ----一对多

CommercialePiece -|-----------(- piecetransaction -)------------|- Transaction

表的结构是这样的

+-----------------------+  +---------------------- + +----------------------+
+ commercialepiece      +  + piecetransaction      + + transaction          +
+-----------------------+  +---------------------- + +---------------- -----+
+ id_piece   -- pk      +  + id_transaction -pk/fk + + id_transaction -- pk +
+ other informations    +  + id_piece       -pk/fk + +  other informations  +
+                       +  + other informations    + +                      +
+-----------------------+  +---------------------- + +----------------------+

现在,映射:

 @Entity(name = "commercialepiece")
    @Table(name = "commercialepiece")
    @Inheritance(strategy = InheritanceType.JOINED)
    public abstract class CommercialePiece {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
     @Column(name = "idpiece")
    private Long idPiece;

     @OneToMany(mappedBy = "commercialepiece", cascade = 
     cascadeType.ALL,orphanRemoval = true)
        Set<CommercialePieceTransaction> commercialePieceTransactions = new 
     HashSet<CommercialePieceTransaction>();


    // Getter and Setter and



public void removeTransaction(Transaction transaction) {
        for (Iterator<CommercialePieceTransaction> iterator = commercialePieceTransactions.iterator(); iterator.hasNext(); ) {
            CommercialePieceTransaction commercialePieceTransaction = iterator.next();
            if (commercialePieceTransaction.getTransaction().equals(transaction)
                    && commercialePieceTransaction.getCommercialepiece().equals(this)
                    ) {
                iterator.remove();
                commercialePieceTransaction.getTransaction().getCommercialePieceTransactions().remove(commercialePieceTransaction);
                commercialePieceTransaction.setTransaction(null);
                commercialePieceTransaction.setCommercialepiece(null);
                break;
            }
        }
    }


     @Override
     public boolean equals(Object o) {


            if (o == this) return true;
            if (!(o instanceof CommercialePiece)) {
                return false;
            }

            CommercialePiece that = (CommercialePiece) o;

            return
                    Objects.equals(this.idPiece, that.idPiece);
        }


        @Override
        public int hashCode() {
            return Objects.hash(idPiece);
        }
    }

++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++

@Entity()
@Table(name = "piecetransaction")
@IdClass(CommercialePieceTransactionId .class)
public class CommercialePieceTransaction {



    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_piece")
    private CommercialePiece commercialepiece;

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_transaction")
    private Transaction transaction;

   @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass())
            return false;

        CommercialePieceTransaction that = (CommercialePieceTransaction) o;
        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hash(commercialepiece, transaction);
    }
}

++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++

@Embeddable
public class CommercialePieceTransactionId implements Serializable {

    // @Column(name = "id_piece")
    private Long commercialepiece;

    //  @Column(name = "id_transaction")
    private Long transaction;

@Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass())
            return false;

        CommercialePieceTransactionId that = (CommercialePieceTransactionId) o;
        return Objects.equals(commercialepiece, that.commercialepiece) &&
                Objects.equals(transaction, that.transaction);
    }

    @Override
    public int hashCode() {
        return Objects.hash(commercialepiece, transaction
        );
    }
}

++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++

@Entity(name = "transaction")
@Table(name = "transaction")
public class Transaction {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idtransaction")
    private Long idTransaction;

@Override
    public String toString() {
        return description;
    }


    @Override
    public boolean equals(Object o) {


        if (o == this) return true;
        if (!(o instanceof Transaction)) {
            return false;
        }

        Transaction that = (Transaction) o;

        boolean equleq = Objects.equals(this.idTransaction, that.idTransaction);
        return equleq;

    }


    @Override
    public int hashCode() {
        return Objects.hash(idTransaction);
    }

当我通过交易坚持使用CommercialePiece时,一切都很好

 @Test
    public void insertCommercialePieceTransaction() {
        CommercialePiece commercialePiece = commercialePieceRepository.findById(1L).get();
        Transaction transaction = transactionRepository.findById(2L).get();


        commercialePiece.addTransaction(transaction);

        commercialePieceRepository.save(commercialePiece);


    }
  

插入       进入           分段交易           (观察,付款,id_piece,id_transaction)       价值观           (?,?,?,?)

但是当我从CommercialePiece中删除交易时,没有任何作用

 @Test
    public void detachTransactionFromCommercialePiece() {
        PurchasePiece commercialePiece = purchasePieceRepository.findById(1L).get();
        Transaction transaction = transactionRepository.findById(1L).get();
        commercialePiece.removeTransaction(transaction);
        purchasePieceRepository.save(commercialePiece);





    }

输出为:

2019-02-09 12:59:05 DEBUG org.hibernate.SQL - 
    select
        commercial0_.id_piece as id_piece1_21_0_,
        commercial0_.id_transaction as id_trans2_21_0_,
        commercial0_.observation as observat3_21_0_,
        commercial0_.tranche as tranche4_21_0_ 
    from
        piecetransaction commercial0_ 
    where
        commercial0_.id_piece=? 
        and commercial0_.id_transaction=?
2019-02-09 12:59:06 DEBUG org.hibernate.SQL - 
    select
        commercial0_.id_piece as id_piece1_21_0_,
        commercial0_.id_transaction as id_trans2_21_0_,
        commercial0_.id_piece as id_piece1_21_1_,
        commercial0_.id_transaction as id_trans2_21_1_,
        commercial0_.observation as observat3_21_1_,
        commercial0_.tranche as tranche4_21_1_ 
    from
        piecetransaction commercial0_ 
    where
        commercial0_.id_piece=?
2019-02-09 12:59:06 DEBUG org.hibernate.SQL - 
    select
        transactio0_.idtransaction as idtransa1_36_0_,
        transactio0_.idaccount as idaccoun7_36_0_,
        transactio0_.transaction_uid as transact8_36_0_,
        transactio0_.datetransaction as datetran2_36_0_,
        transactio0_.description as descript3_36_0_,
        transactio0_.direction as directio4_36_0_,
        transactio0_.idpaymentmethod as idpaymen9_36_0_,
        transactio0_.idperson as idperso10_36_0_,
        transactio0_.idtypetransaction as idtypet11_36_0_,
        transactio0_.value as value5_36_0_,
        transactio0_.transaction_write_date as transact6_36_0_,
        transactio0_.transaction_wuid as transac12_36_0_ 
    from
        transaction transactio0_ 
    where
        transactio0_.idtransaction=?
2019-02-09 12:59:06 WARN  o.h.e.i.StatefulPersistenceContext - HHH000179: Narrowing proxy to class com.abdofx.demo.model.purchase.Supplier - this 

操作中断==

  

级联删除不起作用的人,
   此后,预计将删除数据段事务联接表    因此,piecetransaction不再引用ID为1的交易   id为1的commercialePiece;

所以,她有什么问题,我有4天的时间试图解决这个问题,但没有成功,

我启发了@Vlad Mihalcea的一些想法

  

https://github.com/vladmihalcea/high-performance-java-persistence/blob/master/core/src/test/java/com/vladmihalcea/book/hpjp/hibernate/association/BidirectionalManyAsOneToManyWithoutEmbeddedIdTest.java

     

https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/

0 个答案:

没有答案