Spring Data JPA OrphanRemoval不起作用

时间:2018-10-15 12:06:48

标签: java spring hibernate jpa

使用Spring Data JPA从Set中删除项目时遇到问题。 我有三个实体commercialepiece,commercialepieceIem,sellpiece以这种方式建立关系。 commercialePiece 1 ----------> * commercialepieceitem。 我正在使用Postgresql作为DBMS,

数据库架构为

实体商业物品

    CREATE TABLE public.commercialepieceitem
    (
      idpiece integer NOT NULL,
      idpieceitem integer NOT NULL DEFAULT nextval('commercialepieceitem_idpieceitem_seq'::regclass),
      idproduct integer NOT NULL,
      qty numeric,
      price numeric,
      name_h character varying(255),
      tax numeric,
      discount numeric,
      operationcode integer,
      CONSTRAINT pk_commercialepieceitem PRIMARY KEY (idpiece, idpieceitem),
      CONSTRAINT fk_commerci_affecte_product FOREIGN KEY (idproduct)
          REFERENCES public.product (idproduct) MATCH SIMPLE
          ON UPDATE NO ACTION ON DELETE NO ACTION,
      CONSTRAINT fk_commerci_construit_commerci FOREIGN KEY (idpiece)
          REFERENCES public.commercialepiece (idpiece) MATCH SIMPLE
          ON UPDATE NO ACTION ON DELETE NO ACTION
    )
    WITH (
      OIDS=FALSE
    );
    ALTER TABLE public.commercialepieceitem
      OWNER TO postgres;

实体商业作品

CREATE TABLE public.commercialepiece
(
  idpiece integer NOT NULL DEFAULT nextval('commercialepiece_idpiece_seq'::regclass),
  idtva integer,
  idsession integer,
  numpiece character varying(30),
  datepiece date,
  status character varying(20),
  observation text,
  dateecheance date,
  discount numeric,
  CONSTRAINT pk_commercialepiece PRIMARY KEY (idpiece),
  CONSTRAINT fk_commerci_appliquer_tva FOREIGN KEY (idtva)
      REFERENCES public.tva (idtva) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_commerci_require_comptoir FOREIGN KEY (idsession)
      REFERENCES public.comptoirsession (idsession) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public.commercialepiece
  OWNER TO postgres;

将此一对多映射为双向关系后:

@Entity
@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, fetch = FetchType.EAGER)
    private Set<CommercialePieceItem> pieceItems = new HashSet<>();

// Getter and setters and other attributes removed simplicity purprose
}


@Entity
@Table(name = "commercialepieceitem")
public class CommercialePieceItem  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idpieceitem")
    private Long idPieceItem;

  @ManyToOne()
   @JoinColumn(name = "idpiece")
    private CommercialePiece commercialepiece;

// Getter and setters and other attributes removed simplicity purprose
  }


@Entity
@Table(name = "sellpiece")
@PrimaryKeyJoinColumn(name = "idpiece")
public class SellPiece extends CommercialePiece {

}

我试图使用@OnDelete(action = OnDeleteAction.CASCADE)之类的注释,但是没有任何效果。

那么,删除Orpahs的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

该问题与休眠无关,它是数据库问题。 如您在上面创建表的脚本中所见,

 ON UPDATE NO ACTION ON DELETE NO ACTION --> this let hibernate remove parent, with out removing children

 ON UPDATE NO RESTRICT ON DELETE NO RESTRICT --> this prevent hibernate from removing children.

The Correct solution  is 

ON UPDATE NO CASCADE ON DELETE NO CASCADE