Spring数据JPA InheritanceType.JOINED delete无效

时间:2017-06-17 10:44:48

标签: java spring jpa inheritance

我已经实现了类型" InheritanceType.JOINED"的数据库继承。 我已经扩展了CrudRepository接口来做CRUD操作。实现了一些单元测试。我发现救了&更新工作完美,但删除不工作。那么,是什么让这种情况发生?

以下是代码Unite test:

@RunWith(SpringRunner.class)
@SpringBootTest
public class LineCommandRepoTest {

    @Autowired
    CommandRepository commandRepository;
    @Autowired
    ProduitRepository produitRepository;
    @Autowired
    LineCommandRepository lineCommandRepository;




    public void update() {
        LineCommande lc = lineCommandRepository.findOne(4);
        lc.setQty(BigDecimal.valueOf(2000));
        lc.setRemise(BigDecimal.valueOf(2000));
        lc.setPrice(BigDecimal.valueOf(2000));
       // lineCommandRepository.save(lc);
        lineCommandRepository.save(lc);
        LineCommande lc2 = lineCommandRepository.findOne(4);
        Assert.assertTrue(lc.getPrice().equals(BigDecimal.valueOf(2000)));


    }


    public void insert() {
        Commande commande = commandRepository.findOne(1);
        Product p = produitRepository.findOne(1);
        LineCommande lc = new LineCommande();

        lc.setQty(BigDecimal.ONE);
        lc.setPrice(BigDecimal.ONE);
        lc.setRemise(BigDecimal.ONE);
        lc.setCommande(commande);
        lc.setProduct(p);

        lineCommandRepository.save(lc);
        Assert.assertTrue(lineCommandRepository.exists(lc.getIdLine()));

    }


@Test
    public void delete() {
      lineCommandRepository.delete(4);
        Assert.assertFalse(lineCommandRepository.exists(4));
    }


}

以下是超类的代码:

  import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.ObjectProperty;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.beans.property.SimpleObjectProperty;
    import org.springframework.data.annotation.AccessType;

    import javax.persistence.*;
    import java.math.BigDecimal;


    @Entity
    @Table(name = "linecpiece")
    @Inheritance(strategy = InheritanceType.JOINED)
    @AccessType(AccessType.Type.PROPERTY)
    public abstract class LinePiece {


        private int idLine;
        private ObjectProperty<BigDecimal> qty = new SimpleObjectProperty<BigDecimal>(BigDecimal.ZERO);
        private ObjectProperty<BigDecimal> price = new SimpleObjectProperty<BigDecimal>(BigDecimal.ZERO);
        private ObjectProperty<BigDecimal> remise = new SimpleObjectProperty<BigDecimal>(BigDecimal.ZERO);
        private IntegerProperty tva = new SimpleIntegerProperty();
        private ObjectProperty<BigDecimal> subTotal = new SimpleObjectProperty<BigDecimal>(BigDecimal.ZERO);
       // Getter and setter with annotations
}

儿童类代码

package com.example.model.purchase;
    import com.example.model.Product;
    import javax.persistence.*;

    @Entity
    @Table(name = "linecommande")
    @PrimaryKeyJoinColumn(name = "idlinepiece")
    public class LineCommande extends LinePiece {

        private Commande commande;
        private Product product;


        @ManyToOne
        @JoinColumn(name = "idcommercialepiece")
        public Commande getCommande() {
            return commande;
        }

        public void setCommande(Commande commande) {
            this.commande = commande;
        }

        @ManyToOne
        @JoinColumn(name = "idproduct")
        public Product getProduct() {
            return product;
        }

        public void setProduct(Product product) {
            this.product = product;
        }

    /*
        public static Callback<LineCommande, Observable[]> extractor() {
            return (LineCommande p) -> new Observable[]{p.qtyProperty(), p.priceProperty(), p.subTotalProperty()};
        }*/

    }

1 个答案:

答案 0 :(得分:0)

你需要在lineCommandRepository.delete(4);

之后调用flush() CrudRepository中不存在

flush(),但它是EntityManager中的presenet